Help in Input output C files

Reply

Join Date: Aug 2004
Posts: 4
Reputation: wild_potatos is an unknown quantity at this point 
Solved Threads: 0
wild_potatos wild_potatos is offline Offline
Newbie Poster

Help in Input output C files

 
0
  #1
Aug 27th, 2004
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:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help in Input output C files

 
0
  #2
Aug 27th, 2004
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).
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: Help in Input output C files

 
0
  #3
Aug 27th, 2004
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.
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 4
Reputation: wild_potatos is an unknown quantity at this point 
Solved Threads: 0
wild_potatos wild_potatos is offline Offline
Newbie Poster

Re: Help in Input output C files

 
0
  #4
Aug 27th, 2004
could you dave please give me an example about fgets and sscanf
i would notice that i used structers in my code
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help in Input output C files

 
0
  #5
Aug 27th, 2004
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.   */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 4
Reputation: wild_potatos is an unknown quantity at this point 
Solved Threads: 0
wild_potatos wild_potatos is offline Offline
Newbie Poster

Re: Help in Input output C files

 
0
  #6
Aug 29th, 2004
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"
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help in Input output C files

 
0
  #7
Aug 29th, 2004
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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
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