Thread: output to a file in C

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Question output to a file in C

    I dont know how to send my output to a file in C or C++? anyone can help me.
    Thanks alot.

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
    	FILE * Output;
    	Output = fopen("data.txt", "a");
    	fprintf(Output, "data");
    	fclose(Output);
    	return 0;
    }
    Research file pointers
    The world is waiting. I must leave you now.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Here another simple example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char buffer[BUFSIZ];
    	char filename[] = "c:\\temp\\test.txt";
    	FILE *fp = NULL;
    
    	fp = fopen(filename, "w"); /* open file for reading */
    
    	if(fp == NULL) /* ALWAYS CHECK RETURN VALUE !!! */
    	{
    		printf("Failed to open file for writing\n");
    		return -1;
    	}
    	fputs("Hello world\n", fp); /* write a string to file */
    	fputc('A', fp); /* write a character to file */
    	fclose(fp); /* close file */
    
    	fp = fopen(filename, "a"); /* open file for appending !!! */
    	if(fp == NULL)
    	{
    		printf("Failed to open file for appending\n");
    		return -2;
    	}
    	fprintf(fp, "\nMy age is %d\n", 99); /* write a formatted string to file */
    	fclose(fp);
    
    	fp = fopen(filename, "r"); /* open file for reading */
    	if(fp == NULL)
    	{
    		printf("Failed to open file for reading\n");
    		return -3;
    	}
    	while(fgets(buffer, BUFSIZ, fp) != NULL)
    		printf(buffer);
    	fclose(fp);
    
    	return 0;
    }
    Check the Manual Pages for the following funcitons:
    - fopen - fclose - fprintf - fputs - fputc - fwrite - fread - fgets - etc

  4. #4
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Here's another one:

    http://www.cprogramming.com/tutorial/lesson10.html

    Was right there in front of you the whiole time .
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM