Thread: append to text file

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    append to text file

    append to text file-sales-jpg

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main()
    {
    	FILE *fpstream;
    	char ch[50];
    	
    	printf("Input: ");
    	scanf("%[^\n]", ch);
    
    
    	if ( (fpstream = fopen("sales.txt","a") ) == NULL)
    		printf("Cannot open sales.txt file.");
    	else{
    		fputs(ch,fpstream);
    		fclose(fpstream);
    	}
    	printf("\n");
    	system("pause");
    	return 0;
    }
    How to append string to newline instead of last line?

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Huh? Just output the newline with your data.

    Code:
                fputc('\n', fpstream);
                fpust(ch, fpstream);
    Note: the variable named ch invokes a single character rather than a string. Using it with the meaning of string may become troublesome in the future when you review your code (and don't remember most if it)

  3. #3
    Registered User
    Join Date
    Aug 2012
    Location
    Lagos, Nigeria
    Posts
    17
    Make sure you always close every file stream you open. If you dont, the OS will do it for you, but it is good practice to close it

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    bluechip: there is an fclose call in the code posted.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by bluechip View Post
    Make sure you always close every file stream you open. If you dont, the OS will do it for you, but it is good practice to close it
    It's not the OS that does it, but the C library's exit code.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. append data to file at file's start
    By stefan63 in forum Windows Programming
    Replies: 16
    Last Post: 04-18-2012, 01:27 PM
  2. Append one file to another.
    By guillermoh in forum C Programming
    Replies: 6
    Last Post: 02-04-2008, 12:04 PM
  3. file append
    By rahulsk1947 in forum C Programming
    Replies: 2
    Last Post: 10-30-2007, 01:01 AM
  4. Append to a file
    By Queatrix in forum Windows Programming
    Replies: 4
    Last Post: 07-12-2005, 02:12 AM
  5. append file?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2001, 08:37 AM