| View previous topic :: View next topic |
| Author |
Message |
vegito-93
Joined: 18 Feb 2006 Posts: 12
|
Posted: Wed Mar 15, 2006 9:11 am Post subject: Reading single character off a file? |
|
|
Hi guys. I am trying to put some code in my app so i can do this:
| Code: | | 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? |
|
| Back to top |
|
 |
radad
Joined: 19 May 2004 Posts: 246 Location: Melbourne, Australia
|
Posted: Wed Mar 15, 2006 12:01 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
vegito-93
Joined: 18 Feb 2006 Posts: 12
|
Posted: Thu Mar 16, 2006 5:33 am Post subject: |
|
|
so would it be something like this:
| Code: |
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);
|
|
|
| Back to top |
|
 |
Saotome

Joined: 03 Apr 2004 Posts: 182
|
Posted: Thu Mar 16, 2006 6:37 am Post subject: |
|
|
rather something like this:
| Code: |
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: |
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 |
|
| Back to top |
|
 |
vegito-93
Joined: 18 Feb 2006 Posts: 12
|
Posted: Thu Mar 16, 2006 6:54 am Post subject: |
|
|
| great thanks |
|
| Back to top |
|
 |
Drakonite Site Admin

Joined: 17 Jan 2004 Posts: 989
|
|
| Back to top |
|
 |
vegito-93
Joined: 18 Feb 2006 Posts: 12
|
Posted: Thu Mar 16, 2006 1:03 pm Post subject: |
|
|
hey i just got another quick question.
lets say i got something like this:
| Code: | char data1[256]="hi";
char data2[256]="vegito";
|
i want to put them together in one string. how can i do that? |
|
| Back to top |
|
 |
radad
Joined: 19 May 2004 Posts: 246 Location: Melbourne, Australia
|
Posted: Thu Mar 16, 2006 1:50 pm Post subject: |
|
|
| You should try contacting a website that deals with learning C. Here we are more interested in PS2 specific issues. |
|
| Back to top |
|
 |
vegito-93
Joined: 18 Feb 2006 Posts: 12
|
Posted: Fri Mar 17, 2006 6:54 am Post subject: |
|
|
| sorry i am just new to c++ but i do learn quickly. |
|
| Back to top |
|
 |
Riekistyx
Joined: 18 Mar 2006 Posts: 14
|
Posted: Sat Mar 18, 2006 10:15 am Post subject: |
|
|
Yeah -> no...
Gamedev.net = good
Anyways Ill answer your question.
Harder version
| Code: |
#include <iostream>
using namespace std;
void main()
{
char data1[256]="hi";
char data2[256]="vegito";
char TheData[513];
memcpy(TheData, data1, strlen(data1));
memcpy(&TheData[strlen(data1)], data2, strlen(data2));
memset(&TheData[strlen(data1)+strlen(data2)], 0, 1);
cout << TheData;
}
|
Easier Version :)
| Code: |
#include <iostream>
#include <string>
using namespace std;
void main()
{
char data1[256]="hi";
char data2[256]="vegito";
string TheData = string(data1) + string(data2);
cout << TheData;
}
|
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 |
|
| Back to top |
|
 |
radad
Joined: 19 May 2004 Posts: 246 Location: Melbourne, Australia
|
Posted: Mon Mar 20, 2006 9:40 am Post subject: |
|
|
How about this one:
| Code: | char data1[256]="hi";
char data2[256]="vegito";
char TheData[513];
strcpy(TheData, data1);
strcat(TheData, data2); |
| Quote: | | 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. |
|
| Back to top |
|
 |
Riekistyx
Joined: 18 Mar 2006 Posts: 14
|
Posted: Mon Mar 20, 2006 10:59 am Post subject: |
|
|
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 |
|
| Back to top |
|
 |
Drakonite Site Admin

Joined: 17 Jan 2004 Posts: 989
|
Posted: Mon Mar 20, 2006 12:07 pm Post subject: |
|
|
| radad wrote: | How about this one:
| Code: | char data1[256]="hi";
char data2[256]="vegito";
char TheData[513];
strcpy(TheData, data1);
strcat(TheData, data2); |
| Quote: | | 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 |
|
| Back to top |
|
 |
|