Read in a string from a text file

Reply

Join Date: Mar 2006
Posts: 12
Reputation: chelo77 is an unknown quantity at this point 
Solved Threads: 0
chelo77 chelo77 is offline Offline
Newbie Poster

Read in a string from a text file

 
0
  #1
Apr 2nd, 2006
Need help with a function that reads from a text file usins fscanf function.
example say there is 20 stings that are 60 chars long on their own individual line. Does fscanf() read the whole text file or does it read only up to the carriage return. If it doesn't how can you only read the first string in the text file up to the carriage return using fscanf(), i am using C code.

output of text file

hellothereadkdkdkdkd
aldldjfjbualdfljdlfjdfjdl
aldfl;jlfdljflfldjfldjbyhh

my program does not allow to have spaces in between the 60char string,
so i cannot figure how to read the first string in the text because there is a carriage return after every line so it does not look like one massive string. i know that the type File pointer , points to the beginning of the text file but how do you procedd to read in the first string only?????
Please help!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 1
Reputation: brown_ma is an unknown quantity at this point 
Solved Threads: 0
brown_ma brown_ma is offline Offline
Newbie Poster

Re: Read in a string from a text file

 
0
  #2
Apr 13th, 2006

Hey,

I was reading your message because I was having problems with the same thing. Try this
  1.  
  2. void read_file(char string[60])
  3. {
  4. FILE *fp;
  5. char filename[20];
  6. printf("File to open: \n", &filename );
  7. gets(filename);
  8. fp = fopen(filename, "r"); /* open file for input */
  9.  
  10. if (fp) /* If no error occurred while opening file */
  11. { /* input the data from the file. */
  12. fgets(string, 60, fp); /* read the name from the file */
  13. string[strlen(string)] = '\0';
  14. printf("The name read from the file is %s.\n", string );
  15. }
  16. else /* If error occurred, display message. */
  17. {
  18. printf("An error occurred while opening the file.\n");
  19. }
  20. fclose(fp); /* close the input file */
  21. }

It worked for me. I had used your code to help me out with my assignment, so I'd thought I'd return the favor.

Thank you! Hopefully that works after some variable changing and restructering.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Read in a string from a text file

 
0
  #3
Apr 13th, 2006
>>string[strlen(string)] = '\0';

That may or may not work -- if the string does not contain '\n' the above will cut off the last character that was read. fgets() appends the '\n' ONLY if there is one in the stream -- for example if the buffer is filled up before reaching end-of-line or when the last line in the file does not contain '\n'. So the safest way to do it is to check if the last character is '\n'
  1. int len = strlen(string) -1;
  2. if( string[len] == '\n')
  3. string[len] = '\0';
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC