GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Sep-2006, 21:20
jpxtreme jpxtreme is offline
New Member
 
Join Date: Jul 2006
Posts: 18
jpxtreme is on a distinguished road

How do you read strings from a file...?


Hello everyone, I just would like to ask some questions regarding the FILE output/input. I was able to write strings on a file using fprintf, but how can i read all the strings that i had inserted on it? Is it possible to use looping statements? Could you kindly give me some examples please?

And one more questions, how do you pick strings on a file randomly? say, i have inputted 10 words on the file and wanted to pick 5 random words. Is it possible? it would be great if you kindly post some examples... Sorry i am still learning C. Thanks in advance and God bless to you all!
  #2  
Old 27-Sep-2006, 01:21
niek_e niek_e is offline
Junior Member
 
Join Date: Sep 2006
Posts: 39
niek_e is on a distinguished road

Re: How do you read strings from a file...?


say you called your FILE* fFile, you could try something like this:


CPP / C++ / C Code:
char acBuffer[50]; //fill in the size as big as you need it


// fgets returns NULL at the end of the file
while (fgets( acBuffer, 50, fFile ) != NULL) 
{
    printf("Read from file: %s\n", acBuffer)

}
Last edited by LuciWiz : 27-Sep-2006 at 01:38. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #3  
Old 27-Sep-2006, 02:31
jpxtreme jpxtreme is offline
New Member
 
Join Date: Jul 2006
Posts: 18
jpxtreme is on a distinguished road

Re: How do you read strings from a file...?


Thanks for the reply, it helps me alot. But how do you pick the words from a file randomly?
  #4  
Old 27-Sep-2006, 03:12
baccardi baccardi is offline
Junior Member
 
Join Date: Mar 2006
Posts: 47
baccardi is on a distinguished road

Re: How do you read strings from a file...?


read all the strings to the array and then choose 5 of them randomly
  #5  
Old 28-Sep-2006, 04:40
jpxtreme jpxtreme is offline
New Member
 
Join Date: Jul 2006
Posts: 18
jpxtreme is on a distinguished road

Re: How do you read strings from a file...?


Hi, there. I tried to read all the strings from the file using for loop but it only reads the first line only. I can retrieve all strings from the file by manually read them one by one. How do you read all the strings using loop? Sorry for being a noob.
  #6  
Old 28-Sep-2006, 07:30
baccardi baccardi is offline
Junior Member
 
Join Date: Mar 2006
Posts: 47
baccardi is on a distinguished road

Re: How do you read strings from a file...?


the byte's value -1 denotes the end of the file. So when i find this byte in the text i know that it's the end of the file.
  #7  
Old 28-Sep-2006, 10:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How do you read strings from a file...?


Quote:
Originally Posted by jpxtreme
Hi, there. I tried to read all the strings from the file using for loop but it only reads the first line only. I can retrieve all strings from the file by manually read them one by one. How do you read all the strings using loop? Sorry for being a noob.

In the first response to this thread, niek_e gave an example of a loop that reads multiple lines from the file. Did you try something like that?

If you didn't, that may be OK (that's probably one of the better ways, but there are other ways). How can we tell why your method didn't work if we don't know what you tried.

In other words: Show Us Some Code.

Regards,

Dave
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Download files in c for windows operating system oozsakarya C Programming Language 5 20-Jun-2006 04:33
How to detect end of file with read() function call? nkhambal C Programming Language 6 12-Oct-2004 02:08
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:14.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.