943,536 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4325
  • C RSS
Aug 27th, 2004
0

Help in Input output C files

Expand Post »
Hi guys / girls i am new in this forum and i hope you could help me in this problem.

i programmed a link list which stores some information one of them is strings and i entered them using the function gets(); instead of scanf(); to enable the program to read the space character, and at the termination of the program i call a function to save the linked list to a file but the problem aarise when i return and open the file and load the link list from the file to Main Memmory this what happen:

suppose i entered the string "aaaa bbbb" note that there is a space..
when i close the program and then reexecute it and use the function
fscanf(); to read from a file it consider "aaaa" a string and "bbbb" another string it don't consider them as one string.

please help me.. :rolleyes: :rolleyes: :rolleyes:
Reputation Points: 12
Solved Threads: 0
Newbie Poster
wild_potatos is offline Offline
4 posts
since Aug 2004
Aug 27th, 2004
0

Re: Help in Input output C files

Quote originally posted by wild_potatos ...
Hi guys / girls i am new in this forum and i hope you could help me in this problem.

i programmed a link list which stores some information one of them is strings and i entered them using the function gets(); instead of scanf(); to enable the program to read the space character, and at the termination of the program i call a function to save the linked list to a file but the problem aarise when i return and open the file and load the link list from the file to Main Memmory this what happen:

suppose i entered the string "aaaa bbbb" note that there is a space..
when i close the program and then reexecute it and use the function
fscanf(); to read from a file it consider "aaaa" a string and "bbbb" another string it don't consider them as one string.

please help me.. :rolleyes: :rolleyes: :rolleyes:
Avoid both gets and scanf -- with which it looks like you discovered that the %s specifier is whitespace delimited. Instead use fgets to read a whole line, followed by sscanf to pick off the parameters (or other means since you've already got one big string) -- and don't forget to specify the maximum width with the %s (that is, %10s for example).
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 27th, 2004
0

Re: Help in Input output C files

Yea, better use fstreams they are much better and easier to use.Here's a link:
http://www.daniweb.com/techtalkforums/thread6542.html

Try posting some of your code too.We might be able to help you better then.
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Aug 27th, 2004
0

Re: Help in Input output C files

could you dave please give me an example about fgets and sscanf
i would notice that i used structers in my code
Reputation Points: 12
Solved Threads: 0
Newbie Poster
wild_potatos is offline Offline
4 posts
since Aug 2004
Aug 27th, 2004
0

Re: Help in Input output C files

An example:
  1. #include <stdio.h>
  2.  
  3. struct name
  4. {
  5. char first[16];
  6. char last[32];
  7. };
  8.  
  9. int main(void)
  10. {
  11. char line [ BUFSIZ ];
  12. fputs("Name (First Last)? ", stdout);
  13. fflush(stdout);
  14. if ( fgets(line, sizeof line, stdin) )
  15. {
  16. struct name myname;
  17. if ( sscanf(line, "%15s%31s", myname.first, myname.last) == 2 )
  18. {
  19. printf("myname: first = \"%s\", last = \"%s\"\n",
  20. myname.first, myname.last);
  21. }
  22. }
  23. return 0;
  24. }
  25.  
  26. /* my output
  27.   Name (First Last)? Dave Sinkula
  28.   myname: first = "Dave", last = "Sinkula"
  29.   */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 29th, 2004
0

Re: Help in Input output C files

i am sorry dave but i don't know you may not understanded me or vice versa i want to save the values in a file outside C for example "wild_potatos.dat"
Reputation Points: 12
Solved Threads: 0
Newbie Poster
wild_potatos is offline Offline
4 posts
since Aug 2004
Aug 29th, 2004
0

Re: Help in Input output C files

Files are streams just like stdin and stdout. Surely you can open a file; and surely you could using fprintf instead of printf without much issue -- so what code can you post that does not behave as you'd like?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: variable function parameters functionname(int a, ...)
Next Thread in C Forum Timeline: circular link list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC