Thread: fputs only write first part of string

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    66

    fputs only write first part of string

    I am using fputs like so

    PHP Code:

    strcat
    (linebuf"\n");
    fputs(linebufhistory_stream); 
    where linebuf is a string from stdio and history_stream is a text file.

    instead of writing "hello i am here" to a text file, it simply write "hello", and misses of the rest of the string.

    This a regular problem?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Can I see how you set up the string and the file?

    Does it look like this?

    Code:
       char linebuf[20] = "Hello I am here";
       
       FILE *history_stream;
       history_stream = fopen("test.txt", "w");
       
       strcat(linebuf, "\n");
       fputs(linebuf, history_stream);
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    PHP Code:

    #define MAXLINELEN 4096


    char linebuf[MAXLINELEN];

    FILE *history_stream;

        if ( ( 
    history_stream fopen("history.txt""a+") ) == NULL )
        {
            
    perror("Bash");
        }
    ...

        
    strcat(linebuf"\n");
        
    fputs(linebufhistory_stream);
        
    fflush(history_stream); 
    EDIT: just realised its not the fputs cutting the string down,

    after doing a
    PHP Code:
    fprintf(stdout"%s\n"linebuf); 
    just before fputs, i realised that linebuf has been cut down.
    Last edited by jamie85; 11-17-2005 at 08:13 AM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you read the string stored in linebuf from the user using something like scanf the problem is likely because the scanf call stopped storing the user's input after the first whitespace, in other words, scanf will stop after that first space following the word "hello". You need to use a different method for getting the input from the user, like fgets(linebuf,sizeof(linebuf),stdio) for example. The fgets function will store the newline character so you may have to deal with that.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    66
    Quote Originally Posted by hk_mp5kpdw
    If you read the string stored in linebuf from the user using something like scanf the problem is likely because the scanf call stopped storing the user's input after the first whitespace, in other words, scanf will stop after that first space following the word "hello". You need to use a different method for getting the input from the user, like fgets(linebuf,sizeof(linebuf),stdio) for example. The fgets function will store the newline character so you may have to deal with that.

    I was using fgets however i had this little piece of code

    PHP Code:
    strchr(linebuf'\n');

    if (
    p)
        *
    '\0'
    which was causing me the problem.

    all fixed now thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM