File Handling -Read write and edit a file

This is a discussion on File Handling -Read write and edit a file within the C Programming forums, part of the General Programming Boards category; Hi guys im trying to get a program to read a file and copy its contents to another file so ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    18

    File Handling -Read write and edit a file

    Hi guys im trying to get a program to read a file and copy its contents to another file so far I seem to be having a problem.
    The file is only writing one letter, "X" in this case
    Im just wondering what my problem is here?
    the file im testing it on is just a massive text document, so there dhould be more than one letter comming out
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main ( int argc, char *argv[] )
    {
        if ( argc != 2 )
        {
    
            printf( "usage: %s filename", argv[0] );
        }
        else
        {
    
            FILE *file = fopen( argv[1], "r" );
    
    
            if ( file == 0 )
            {
                printf( "Could not open file\n" );
            }
            else
            {
                int x;
    
                while  ( ( x = fgetc( file ) ) != EOF )
                {
                    FILE *outf;
    	//if (isspace(x)&&isspace(x-1)
    	//remove space
    	//else
    	outf = fopen("test.txt","w");
    
    
    	fprintf(outf,"%c");
        fclose(outf);
    
                }
    
            }
    
            fclose( file );
        }
    }
    Eventually im going to use this program to tidy up a text document with float values, that sounds confusing as I type it, but its just a text file with numeric data on it but its very messed up as it looks something like this
    Code:
    121.2       12.12 121.21         1.212 
    1.12 121 121 1        1.212    12.12      121.2 12
    ect....
    What I want to do is tidy it up so that there is only one space between each value and hopefully start a new line after every 10 numbers.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Code:
    fprintf(outf,"%c");
    You're only printing a single character here, and what's even worse, you haven't even supplied an address to print so the value is going to be some random value from the stack.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    18
    [QUOTE=Memloop;926408]
    Code:
    fprintf(outf,"%c");
    You're only printing a single character here, and what's even worse, you haven't even supplied an address to print so the value is going to be some random value from the stack.[/QUOT]

    But I thought once inside the while loop until it gets to the EOF it will keep adding to the file?
    Also there is no 'X' in my text file?
    How do I correct this?

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    > But I thought once inside the while loop until it gets to the EOF it will keep adding to the file?
    > Also there is no 'X' in my text file?
    Yes that is correct (I missed it because of the horrible indentation you use).

    > Also there is no 'X' in my text file?
    The "%c" specifier in the printf family requires you to supply a char parameter (and you haven't). What exactly do you expect to print when you haven't told fprintf which variable to print?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doesn't write in file when call GetSaveFileName()
    By randall81 in forum Windows Programming
    Replies: 1
    Last Post: 03-28-2009, 01:34 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. How to go into middle of file and read then write
    By Garfield in forum C Programming
    Replies: 4
    Last Post: 01-15-2002, 12:57 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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