Reading single character off a file?

Discuss the development of software, tools, libraries and anything else that helps make ps2dev happen.

Moderators: cheriff, Herben

Locked
vegito-93
Posts: 12
Joined: Sat Feb 18, 2006 10:36 am

Reading single character off a file?

Post by vegito-93 »

Hi guys. I am trying to put some code in my app so i can do this:

Code: Select all

hello my name is vegito
lets say i got that inside a .cnf file on my mc. I open it up with fioOpen and now i want to read from it. I don't want to read the whole sentence but only a single character from it. So lets say i want to read the fifth character from the file. I this case it would be o. How can i do that?
radad
Posts: 246
Joined: Wed May 19, 2004 4:54 pm
Location: Melbourne, Australia

Post by radad »

You use a seek function to position the file pointer. Then use the read function to read just one byte. (Don't have the exact function names in front of me).

Generally though it would be easier to read the whole thing into memory and use memory access for individual characters.
vegito-93
Posts: 12
Joined: Sat Feb 18, 2006 10:36 am

Post by vegito-93 »

so would it be something like this:

Code: Select all

unsigned char *data;
fd = fioOpen("mc0:/SYS-CONF/hello.cnf", O_RDONLY);
seek = fioLseek(fd, 0, SEEK_END); //but the zero here. what should i replace it with?
fioLseek(fd, 0, SEEK_SET);// same question here
fioRead(fd,data,seek);
fioClose(fd);
printf("the fifth byte is %d", data);
User avatar
Saotome
Posts: 182
Joined: Sat Apr 03, 2004 3:45 am

Post by Saotome »

rather something like this:

Code: Select all

unsigned char data; 
fd = fioOpen("mc0:/SYS-CONF/hello.cnf", O_RDONLY); 
position_of_character = 4; //or any other value
fioLseek(fd, position_of_character, SEEK_SET);
fioRead(fd,&data,1); 
fioClose(fd); 
printf("the fifth byte is %d", data);
;)
but as radad already said, it would be better (faster) to do something like this:

Code: Select all

unsigned char data[256]; 
fd = fioOpen("mc0:/SYS-CONF/hello.cnf", O_RDONLY); 
fioRead(fd,data,256); 
fioClose(fd); 
position_of_character = 4; //or any other value
printf("the fifth byte is %d", data[position_of_character]);
infj
vegito-93
Posts: 12
Joined: Sat Feb 18, 2006 10:36 am

Post by vegito-93 »

great thanks
User avatar
Drakonite
Site Admin
Posts: 990
Joined: Sat Jan 17, 2004 1:30 am
Contact:

Post by Drakonite »

Shoot Pixels Not People!
Makeshift Development
vegito-93
Posts: 12
Joined: Sat Feb 18, 2006 10:36 am

Post by vegito-93 »

hey i just got another quick question.
lets say i got something like this:

Code: Select all

char data1[256]="hi";
char data2[256]="vegito";
  
i want to put them together in one string. how can i do that?
radad
Posts: 246
Joined: Wed May 19, 2004 4:54 pm
Location: Melbourne, Australia

Post by radad »

You should try contacting a website that deals with learning C. Here we are more interested in PS2 specific issues.
vegito-93
Posts: 12
Joined: Sat Feb 18, 2006 10:36 am

Post by vegito-93 »

sorry i am just new to c++ but i do learn quickly.
Riekistyx
Posts: 14
Joined: Sat Mar 18, 2006 9:56 am

Post by Riekistyx »

Yeah -> no...

Gamedev.net = good

Anyways Ill answer your question.

Harder version

Code: Select all

#include <iostream>
using namespace std;

void main&#40;&#41;
&#123;
	char data1&#91;256&#93;="hi"; 
	char data2&#91;256&#93;="vegito"; 
	char TheData&#91;513&#93;;

	memcpy&#40;TheData, data1, strlen&#40;data1&#41;&#41;;
	memcpy&#40;&TheData&#91;strlen&#40;data1&#41;&#93;, data2, strlen&#40;data2&#41;&#41;;
	memset&#40;&TheData&#91;strlen&#40;data1&#41;+strlen&#40;data2&#41;&#93;, 0, 1&#41;;
	cout << TheData;

&#125;
Easier Version :)

Code: Select all

#include <iostream>
#include <string>
using namespace std;

void main&#40;&#41;
&#123;
	char data1&#91;256&#93;="hi"; 
	char data2&#91;256&#93;="vegito"; 
	string TheData = string&#40;data1&#41; + string&#40;data2&#41;;
	cout << TheData;
&#125;
This was compiled in vsnet 2005. I hope this aids you in c++. Remeber one thing there is no such thing as strings in C++ :D
LOL @ linux users
radad
Posts: 246
Joined: Wed May 19, 2004 4:54 pm
Location: Melbourne, Australia

Post by radad »

How about this one:

Code: Select all

char data1&#91;256&#93;="hi"; 
   char data2&#91;256&#93;="vegito"; 
   char TheData&#91;513&#93;; 

   strcpy&#40;TheData, data1&#41;;
   strcat&#40;TheData, data2&#41;;
Remeber one thing there is no such thing as strings in C++ :D
And what does that mean? Strings exist as much as they do in C.
Riekistyx
Posts: 14
Joined: Sat Mar 18, 2006 9:56 am

Post by Riekistyx »

That method words as well

If you read harder in real coding books there is "no such thing as a string in C++"

If there was then I should be able to do this "dingle" + " berry"

=D
LOL @ linux users
User avatar
Drakonite
Site Admin
Posts: 990
Joined: Sat Jan 17, 2004 1:30 am
Contact:

Post by Drakonite »

radad wrote:How about this one:

Code: Select all

char data1&#91;256&#93;="hi"; 
   char data2&#91;256&#93;="vegito"; 
   char TheData&#91;513&#93;; 

   strcpy&#40;TheData, data1&#41;;
   strcat&#40;TheData, data2&#41;;
Remeber one thing there is no such thing as strings in C++ :D
And what does that mean? Strings exist as much as they do in C.
They are arrays of chars, not strings. And actually, strings exist more in C++ than in C, but the real problem is I don't think everyone in this thread understands that C and C++ are different languages.

I take that back, the real problem is that this is a very beginner programming thread on a much more specialized forum. This thread is only going to go no where, so I'm locking it.
Shoot Pixels Not People!
Makeshift Development
Locked