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 file line by line

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
uber0ne



Joined: 14 Jun 2006
Posts: 4

PostPosted: Wed Jun 14, 2006 4:28 pm    Post subject: Reading file line by line Reply with quote

Ok here what i want to do. I have a file named myfile.txt i want to pick a random number and have it read that line of the file.
EX.
hhhhh
jjjjjjjj
kkkkk
lllllllllll
yyyyy
tttttttt
and i want to pull out line 4 and store it in a variable so that
variable == lllllllllll
If this would be easier with an ini then please tell me.
Back to top
View user's profile Send private message Visit poster's website AIM Address
groepaz



Joined: 01 Sep 2005
Posts: 305

PostPosted: Wed Jun 14, 2006 4:52 pm    Post subject: Reply with quote

checkout fscanf, that should do the trick
_________________
http://www.hitmen-console.org
http://hitmen.c02.at/files/yapspd/
Back to top
View user's profile Send private message Visit poster's website
uber0ne



Joined: 14 Jun 2006
Posts: 4

PostPosted: Wed Jun 14, 2006 4:59 pm    Post subject: Reply with quote

i want to be able to tell the program line number 4 and it goes to line number 4 and pulls out the file or and number
Back to top
View user's profile Send private message Visit poster's website AIM Address
groepaz



Joined: 01 Sep 2005
Posts: 305

PostPosted: Wed Jun 14, 2006 5:02 pm    Post subject: Reply with quote

i dont think there are ready made functions that do exactly this...so you have to parse the file yourself. in most cases you might want to keep it buffered in ram anyway.
_________________
http://www.hitmen-console.org
http://hitmen.c02.at/files/yapspd/
Back to top
View user's profile Send private message Visit poster's website
uber0ne



Joined: 14 Jun 2006
Posts: 4

PostPosted: Wed Jun 14, 2006 5:21 pm    Post subject: Reply with quote

yea but how would i do that....
Back to top
View user's profile Send private message Visit poster's website AIM Address
Drakonite
Site Admin


Joined: 17 Jan 2004
Posts: 989

PostPosted: Wed Jun 14, 2006 7:38 pm    Post subject: Reply with quote

Learning C would be a good start.
_________________
Shoot Pixels Not People!
Makeshift Development
Back to top
View user's profile Send private message Visit poster's website
sg57



Joined: 14 Oct 2005
Posts: 154

PostPosted: Thu Jun 15, 2006 12:04 pm    Post subject: Reply with quote

Just as i had said and psoted on psp-programming.com...
Code:

#include <stdio.h>
// what line to go to: 5 as example
#define GotoLine 5

// do not touch below
#define lineNumber 0

int main() {
           
           char string[1000];
           int z;
           FILE * pFile;
 
           long lSize; 
           char * buffer;

           pFile = fopen ( "myfile.txt" , "rb"); 
           
           printf("File Opened Successuflly\n");
           
           fseek (pFile , 0 , SEEK_END);
 
           lSize = ftell (pFile);
 
           rewind (pFile);
 
           buffer = (char*) malloc (lSize);
           
           for ( z=lineNumber; z<GotoLine; z++ ) {
                 
           fgets(buffer, lSize, pFile);
           }

           printf("%s", buffer);
           
           getch();
return 0;
}

Complied and tested WORKING via Dev-C++, if you wish to use this on the PSP, change getch(); to the sceKIernelSleepThread(); or delay, or whatever... Also, as you cna tell, the line number being read is fed into the buffer named 'buffer' so...

oh and to get a random line number from the file, then do this, since at psp-rpogramming you asked for random, AKA doing your ENTIRE code for you :(
Code:
#include <stdio.h>

#define GotoLine rand()%100

int main() {
           
           char string[1000];
           int lineNumber = 0, z;
           FILE * pFile;
 
           long lSize; 
           char * buffer;

           srand(time());

           pFile = fopen ( "myfile.txt" , "rb"); 
           
           printf("File Opened Successuflly\n");
           
           fseek (pFile , 0 , SEEK_END);
 
           lSize = ftell (pFile);
 
           rewind (pFile);
 
           buffer = (char*) malloc (lSize);
           
           for ( z=lineNumber; z<GotoLine; z++ ) {
                 
           fgets(buffer, lSize, pFile);
           }

           printf("%s", buffer);
           
           
           getch();
return 0;
}
Back to top
View user's profile Send private message
boomint



Joined: 13 Apr 2004
Posts: 80
Location: Sheffield, UK

PostPosted: Thu Jun 15, 2006 5:04 pm    Post subject: Reply with quote

You may also want inform him that GotoLine definition assumes there are 100 lines in the file...

sg57 wrote:
Code:

#define GotoLine rand()%100


And leave populating this with the actual number of lines in the file as an exercise for the reader ;)
_________________
--( someone stole my sig! )--
Back to top
View user's profile Send private message
sg57



Joined: 14 Oct 2005
Posts: 154

PostPosted: Fri Jun 16, 2006 12:21 am    Post subject: Reply with quote

well at psp-programming.com he had asked for something that will read a file line by line, when having like 5 lines in it, so i just assumed 100 for guessing. And theres a way to first get the number of lines in a file then have it done automaticaly... Use a loop,

while ( gets(file) 1= '\0') { puts(file); LineCount++; }
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP 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