Help with printing to a file!!!!

This is a discussion on Help with printing to a file!!!! within the C Programming forums, part of the General Programming Boards category; I want to print to a .txt file all 25 numbers, but in the file only the 0 apears. How ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    Help with printing to a file!!!!

    I want to print to a .txt file all 25 numbers, but in the file only the 0 apears.
    How can I print consecutive outputs?
    Code:
    #include<stdio.h>
    int main()
    {
    	int j;
    	FILE *textcode;
    	for(j=0; j<26; j++)
    		{
    			textcode=fopen("/home/julian/Desktop/textcode.txt", "w");
    			fprintf(textcode, "%d\t",j);
    		}
    return(0);
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Move fopen to the outside of the loop, test if the file was successfully opened, then enter the loop. And lastly, close the file before returning.

  3. #3
    Registered User krishnapollu's Avatar
    Join Date
    Dec 2010
    Location
    KERALA
    Posts
    29
    Whenever u open a file in "write" mode, its contents will be lost. Here, in this code, u are opening the file in each n evry iteration, which will erase the content that u entered last time. So u should place the code for opening the file, outside the for loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

Tags for this Thread


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