Page 1 of 1

Reading single character off a file?

Posted: Wed Mar 15, 2006 9:11 am
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?

Posted: Wed Mar 15, 2006 12:01 pm
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.

Posted: Thu Mar 16, 2006 5:33 am
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);

Posted: Thu Mar 16, 2006 6:37 am
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]);

Posted: Thu Mar 16, 2006 6:54 am
by vegito-93
great thanks

Posted: Thu Mar 16, 2006 6:56 am
by Drakonite

Posted: Thu Mar 16, 2006 1:03 pm
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?

Posted: Thu Mar 16, 2006 1:50 pm
by radad
You should try contacting a website that deals with learning C. Here we are more interested in PS2 specific issues.

Posted: Fri Mar 17, 2006 6:54 am
by vegito-93
sorry i am just new to c++ but i do learn quickly.

Posted: Sat Mar 18, 2006 10:15 am
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

Posted: Mon Mar 20, 2006 9:40 am
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.

Posted: Mon Mar 20, 2006 10:59 am
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

Posted: Mon Mar 20, 2006 12:07 pm
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.