String Writing Trouble

This is a discussion on String Writing Trouble within the C Programming forums, part of the General Programming Boards category; My request is fairly simple, although Im sure the answer is somewhere in one of the thousands of threads I ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    7

    String Writing Trouble

    My request is fairly simple, although Im sure the answer is somewhere in one of the thousands of threads I just havent located yet but here goes. I am designing a program a bit like notpad except with a few more capabilities (i.e. deleting/renaming entire programs and such). All of the major functions except for "writing to file" have been completed. I can write to files, but I can only write one string to a file. For example if I were to Write "Hello_World" to a file it displays the entire thing in the text file, however, when I input it as "Hello Word" it only writes Hello to the file. Here is my code for the "write to file" function:
    Code:
    #include <stdio.h>
    
    main()
    {
    
    FILE *file;
    char input[80];
    char filename[]="test1.txt;
    
    printf ("Opening %s.\n",filename);
    if ((file=fopen(filename,"r+"))==NULL)
    {
    printf ("Unable to open specified file.\n");
    return 0;
    }
    else
    {
    scanf ("%s",input);
    fputs (input,file);
    }
    fclose(file)
    }
    ---------------------------------------------------------------------------------

    Granted this isnt the entire code for the function, just the important parts. The part in red is where I am having trouble. I know that fputs() is used to write one string at a time but I have seen it be done where you could input what I suppose you would call two strings, or rather I have seen the opposite where you can read multiple strings at a time from a file such as "Hello" "World". I have fail to bring out the reverse outcome so I am asking for help.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    scanf with %s just reads one word - use fgets() to read a whole line in one go.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Could you elaborate on that?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Change
    Code:
    scanf ("%s",input);
    to
    Code:
    fgets(input, sizeof(input), stdin);
    and then you can read in strings containing whitespace.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Ok, I think I got it. I just noticed something too. I could have also done it like:

    Code:
    for(round=0;round<500;round++)
    {
    gets(write);
    fputs(write,file);
    if(!strcoll(write,"end"))
    {
    return 0;
    fclose(file)
    }
    }
    --------------------------------------------------------------------------------------------

    Round would be the number of times I could hit the enter key and advance to the next line, and if the string "end" was entered into any of those rounds it would automatically exit. On Windows the round variable wouldnt be needed because Windows automatically wraps around the string to the next line if you hit the edge of the console. Macs dont provide that luxury, at least not version 9.2.2, so the round variable is useful.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    gets(write);
    Read the FAQ on why this is bad.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    I accidently put the return 0 in front of the fclose(), my mistake. *doh*

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    I didnt realize that it wasnt a good idea to use the gets(). The only thing I have ever heard said was a bad idea to use was the goto statement because it creates "spagetti code". I cant find the thread that states why it is a bad idea to use gets(), so I would be grateful if you could tell me which thread it is.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's in the FAQ. Specifically, right here. When someone here mentions the FAQ, they're usually talking about that little link at the top of the page.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Oh, so the reason most programmers try to avoid using it is because you might accidently exceed the number of bytes you can enter into an input. Come to think of it I did run into something like that on my first test phase of the "write to file" function,I figured it was nothing a few well placed safety precaution pieces of code couldnt fix.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    I figured it was nothing a few well placed safety precaution pieces of code couldnt fix.
    There is. Use fgets() instead of gets().
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    I will propably change it to that when I update my program later on. In the mean time Im simply checking to make sure everything is working in order and it seems to be doing fine.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It seems to be doing fine . . . until you enter too long a string.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in writing string to File
    By Bargi in forum C Programming
    Replies: 4
    Last Post: 02-18-2009, 11:05 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21