forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reading single character off a file?

 
Post new topic   This topic is locked: you cannot edit posts or make replies.    forums.ps2dev.org Forum Index -> PS2 Development
View previous topic :: View next topic  
Author Message
vegito-93



Joined: 18 Feb 2006
Posts: 12

PostPosted: Wed Mar 15, 2006 9:11 am    Post subject: Reading single character off a file? Reply with quote

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
View user's profile Send private message
radad



Joined: 19 May 2004
Posts: 246
Location: Melbourne, Australia

PostPosted: Wed Mar 15, 2006 12:01 pm    Post subject: Reply with quote

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
View user's profile Send private message
vegito-93



Joined: 18 Feb 2006
Posts: 12

PostPosted: Thu Mar 16, 2006 5:33 am    Post subject: Reply with quote

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
View user's profile Send private message
Saotome



Joined: 03 Apr 2004
Posts: 182

PostPosted: Thu Mar 16, 2006 6:37 am    Post subject: Reply with quote

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
View user's profile Send private message
vegito-93



Joined: 18 Feb 2006
Posts: 12

PostPosted: Thu Mar 16, 2006 6:54 am    Post subject: Reply with quote

great thanks
Back to top
View user's profile Send private message
Drakonite
Site Admin


Joined: 17 Jan 2004
Posts: 989

PostPosted: Thu Mar 16, 2006 6:56 am    Post subject: Reply with quote

I would recommend The C Programming Language and http://www.gamedev.net
_________________
Shoot Pixels Not People!
Makeshift Development
Back to top
View user's profile Send private message Visit poster's website
vegito-93



Joined: 18 Feb 2006
Posts: 12

PostPosted: Thu Mar 16, 2006 1:03 pm    Post subject: Reply with quote

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
View user's profile Send private message
radad



Joined: 19 May 2004
Posts: 246
Location: Melbourne, Australia

PostPosted: Thu Mar 16, 2006 1:50 pm    Post subject: Reply with quote

You should try contacting a website that deals with learning C. Here we are more interested in PS2 specific issues.
Back to top
View user's profile Send private message
vegito-93



Joined: 18 Feb 2006
Posts: 12

PostPosted: Fri Mar 17, 2006 6:54 am    Post subject: Reply with quote

sorry i am just new to c++ but i do learn quickly.
Back to top
View user's profile Send private message
Riekistyx



Joined: 18 Mar 2006
Posts: 14

PostPosted: Sat Mar 18, 2006 10:15 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail AIM Address
radad



Joined: 19 May 2004
Posts: 246
Location: Melbourne, Australia

PostPosted: Mon Mar 20, 2006 9:40 am    Post subject: Reply with quote

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
View user's profile Send private message
Riekistyx



Joined: 18 Mar 2006
Posts: 14

PostPosted: Mon Mar 20, 2006 10:59 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail AIM Address
Drakonite
Site Admin


Joined: 17 Jan 2004
Posts: 989

PostPosted: Mon Mar 20, 2006 12:07 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   This topic is locked: you cannot edit posts or make replies.    forums.ps2dev.org Forum Index -> PS2 Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group