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:

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).

could you dave please give me an example about fgets and sscanf
i would notice that i used structers in my code

An example:

#include <stdio.h>
  
  struct name
  {
     char first[16];
     char last[32];
  };
  
  int main(void)
  {
     char line [ BUFSIZ ];
     fputs("Name (First Last)? ", stdout);
     fflush(stdout);
     if ( fgets(line, sizeof line, stdin) )
     {
  	  struct name myname;
  	  if ( sscanf(line, "%15s%31s", myname.first, myname.last) == 2 )
  	  {
  		 printf("myname: first = \"%s\", last = \"%s\"\n",
  				myname.first, myname.last);
  	  }
     }
     return 0;
  }
  
  /* my output
  Name (First Last)? Dave Sinkula
  myname: first = "Dave", last = "Sinkula"
  */

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"

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?

This article has been dead for over six months. Start a new discussion instead.