using gets() to output a string to a file in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jonniethecodeprince
    New Member
    • Mar 2007
    • 35

    using gets() to output a string to a file in C

    Hello all,

    I am trying to read in some data which input from the keyboard which is stored using data structures. I have found a way to get this to work using the first input from the user in a set of questions in this code.

    [code="c"]printf("\n\nEnt er your name: ");
    gets(personalDe tails.criminalN ame);
    puts(personalDe tails.criminalN ame);[/code]

    Later on I have a similar set of code which is used in a similar way only further on in the program.
    I have to use the scanf() function this time so the compiler will stop and allow me to input the info needed, otherwise it skims right over to the next input

    [code="c"]printf("\nHouse Name Please: ");
    scanf("%s", &address.houseN ame);
    gets(address.ho useName);
    puts(address.ho useName);[/code]

    Here if I input Horns House Farm it only displays House Farm. Can anyone give me a clue as to how to resolve this even if not with a full code solution?

    thank you,

    Jonathan
  • questionit
    Contributor
    • Feb 2007
    • 553

    #2
    Hi Jonnie

    Try removing the Ampersand sign (&) in the scanf

    [code=C]
    scanf("%s", address.houseNa me);
    [/code]

    I am not too sure why you are taking input in address.houseNa me twice, once with scanf and second time with gets. What exactly you are trying to do?
    Any second attempt to write in a variable for the 2nd time, weather it be with gets or scanf, the previous input will be over-written.

    Qi

    Originally posted by jonniethecodepr ince
    Hello all,

    I am trying to read in some data which input from the keyboard which is stored using data structures. I have found a way to get this to work using the first input from the user in a set of questions in this code.

    [code="c"]printf("\n\nEnt er your name: ");
    gets(personalDe tails.criminalN ame);
    puts(personalDe tails.criminalN ame);[/code]

    Later on I have a similar set of code which is used in a similar way only further on in the program.
    I have to use the scanf() function this time so the compiler will stop and allow me to input the info needed, otherwise it skims right over to the next input

    [code="c"]printf("\nHouse Name Please: ");
    scanf("%s", &address.houseN ame);
    gets(address.ho useName);
    puts(address.ho useName);[/code]

    Here if I input Horns House Farm it only displays House Farm. Can anyone give me a clue as to how to resolve this even if not with a full code solution?

    thank you,

    Jonathan

    Comment

    • jonniethecodeprince
      New Member
      • Mar 2007
      • 35

      #3
      [code=c]
      printf("\nHouse Name Please: ");
      scanf("%s", &address.houseN ame);
      gets(address.ho useName);
      puts(address.ho useName);

      Originally posted by questionit
      I am not too sure why you are taking input in address.houseNa me twice, once with scanf and second time with gets. What exactly you are trying to do?
      Any second attempt to write in a variable for the 2nd time, weather it be with gets or scanf, the previous input will be over-written.

      Qi
      I did this because If I comment out the scanf() statement the compiler will completely skim over the printf prompt for the house name as well as the gets() statement. The scanf statement was the only way I could stop this from happening and capture data from the keyboard after the printf statement. .

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by jonniethecodepr ince

        [code=c]
        printf("\nHouse Name Please: ");
        scanf("%s", &address.houseN ame);
        gets(address.ho useName);
        puts(address.ho useName);
        [/code]



        I did this because If I comment out the scanf() statement the compiler will completely skim over the printf prompt for the house name as well as the gets() statement. The scanf statement was the only way I could stop this from happening and capture data from the keyboard after the printf statement. .
        You need to 'flush' your input stream.Wrong thing to do is to call fflush because
        it is defined for output streams only.Also avoid gets,it is deprecated use fgets instead.To manually flush the stream you can use this code:

        [CODE=c]

        //Flush it
        while((c=fgetc( stdin))&&c!='\n ');

        //proceed with input[/CODE]

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          If you are looking for better description what is happening check this


          Savage

          Comment

          • jonniethecodeprince
            New Member
            • Mar 2007
            • 35

            #6
            Thanks people. It seems to have worked so far. But with C,we all know one problem can be substituted with another So I may be back with more headaches lol

            Comment

            Working...