Reading file line by line

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
uber0ne
Posts: 4
Joined: Wed Jun 14, 2006 4:23 pm
Contact:

Reading file line by line

Post by uber0ne »

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.
User avatar
groepaz
Posts: 305
Joined: Thu Sep 01, 2005 7:44 am
Contact:

Post by groepaz »

checkout fscanf, that should do the trick
uber0ne
Posts: 4
Joined: Wed Jun 14, 2006 4:23 pm
Contact:

Post by uber0ne »

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
User avatar
groepaz
Posts: 305
Joined: Thu Sep 01, 2005 7:44 am
Contact:

Post by groepaz »

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.
uber0ne
Posts: 4
Joined: Wed Jun 14, 2006 4:23 pm
Contact:

Post by uber0ne »

yea but how would i do that....
User avatar
Drakonite
Site Admin
Posts: 990
Joined: Sat Jan 17, 2004 1:30 am
Contact:

Post by Drakonite »

Learning C would be a good start.
Shoot Pixels Not People!
Makeshift Development
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

Post by sg57 »

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

Code: Select all

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

// do not touch below
#define lineNumber 0

int main&#40;&#41; &#123; 
            
           char string&#91;1000&#93;; 
           int z; 
           FILE * pFile; 
  
           long lSize;  
           char * buffer;

           pFile = fopen &#40; "myfile.txt" , "rb"&#41;;  
            
           printf&#40;"File Opened Successuflly\n"&#41;; 
            
           fseek &#40;pFile , 0 , SEEK_END&#41;; 
  
           lSize = ftell &#40;pFile&#41;; 
  
           rewind &#40;pFile&#41;; 
  
           buffer = &#40;char*&#41; malloc &#40;lSize&#41;; 
            
           for &#40; z=lineNumber; z<GotoLine; z++ &#41; &#123; 
                  
           fgets&#40;buffer, lSize, pFile&#41;; 
           &#125; 

           printf&#40;"%s", buffer&#41;; 
            
           getch&#40;&#41;; 
return 0; 
&#125; 
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: Select all

#include <stdio.h> 

#define GotoLine rand&#40;&#41;%100 

int main&#40;&#41; &#123; 
            
           char string&#91;1000&#93;; 
           int lineNumber = 0, z; 
           FILE * pFile; 
  
           long lSize;  
           char * buffer; 

           srand&#40;time&#40;&#41;&#41;; 

           pFile = fopen &#40; "myfile.txt" , "rb"&#41;;  
            
           printf&#40;"File Opened Successuflly\n"&#41;; 
            
           fseek &#40;pFile , 0 , SEEK_END&#41;; 
  
           lSize = ftell &#40;pFile&#41;; 
  
           rewind &#40;pFile&#41;; 
  
           buffer = &#40;char*&#41; malloc &#40;lSize&#41;; 
            
           for &#40; z=lineNumber; z<GotoLine; z++ &#41; &#123; 
                  
           fgets&#40;buffer, lSize, pFile&#41;; 
           &#125; 

           printf&#40;"%s", buffer&#41;; 
            
            
           getch&#40;&#41;; 
return 0; 
&#125; 
boomint
Posts: 80
Joined: Tue Apr 13, 2004 2:21 am
Location: Sheffield, UK

Post by boomint »

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

Code: Select all

#define GotoLine rand&#40;&#41;%100 
And leave populating this with the actual number of lines in the file as an exercise for the reader ;)
--( someone stole my sig! )--
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

Post by sg57 »

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++; }
Post Reply