append to text file

This is a discussion on append to text file within the C Programming forums, part of the General Programming Boards category; 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") ...

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

    append to text file

    Name:  sales.jpg
Views: 232
Size:  38.5 KB

    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
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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
    21,772
    bluechip: there is an fclose call in the code posted.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11:04 AM
  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

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