File problem

This is a discussion on File problem within the C Programming forums, part of the General Programming Boards category; I have this code, but it doesn't work. It should read a number from a file and print it back, ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    124

    File problem

    I have this code, but it doesn't work. It should read a number from a file and print it back, in the same file, multiplied by ten.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int  num;
    	FILE *in;
    
    	in=fopen("test.txt","r+");
    	fscanf( in, "%d", &num );
    	fprintf( in, "%d", num * 10 );
    
    	fclose( in );
    
    	return 0;
    }
    Can you help me find the error?

    Also, if a program that reads charcter by character from a file, prints back that character changed in the same file, will the next characters of the file be lost.? For example a file contains 10 characters. You read the first one, you change it, and you print it back in the same file( and you want to procceed in the nect character ). The next 9 characters of the file will be lost?

    Thanks
    Loading.....
    ( Trying to be a good C Programmer )

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Verify that you are actually opening the file by checking the value of in (initialize it to NULL or I'll kill you) The file "test.txt" will have to exist and be in the same directory as the .exe. Screw it, it's almost 3:00 am, goodnight.
    Away.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    The file exists and is in the same directory with the .exe and it opens.
    Any better ides please?
    Loading.....
    ( Trying to be a good C Programmer )

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Salem, i compile you'r code and it compiles fine. However when i run it makes a problem and tells me: "Unhandled Exception".

    ??

    Also:
    fflush(in);
    fseek(in,readpos,SEEK_SET);

    I think that these are not needed. Do they?

    Also:
    readpos = ftell(in); // where we need to get back to for reading
    fseek(in,writepos,SEEK_SET);

    rewind( in ) would so the same work, isn't it?

    Also:
    What about the second part of my initiall question?
    Thanks again.
    Loading.....
    ( Trying to be a good C Programmer )

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    1)Many thanks, i just forgot to change the name of the file.. It works fine only if in the file there is just the number. When i wrote the file, i wrote the number and i wrote and an ENTER. When the programm works, it prints the right number and a box in black at the position of the enter. Why that ( if there isn't enter it doesn't print that box ) ?

    2) I tried to make it in a more general form. This code should print all the characters uppercase:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main()
    {
    	int a;
    	long readpos, writepos;
    	FILE *in;
    
    	in=fopen("test.txt","r+");
    	writepos = ftell(in);
    	while( ( a = getc( in ) ) != feof( in )){
    		readpos = ftell(in);    // where we need to get back to for reading
    		fseek(in,writepos,SEEK_SET);   // go back to the writepos
    		putc( toupper( a ), in );
    		fflush(in);  
    		fseek(in,readpos,SEEK_SET);     // go back to the readpos
    	}
    	fclose( in );
    
    	return 0;
    }
    But it doesn't work. Why?

    Also, can you explain me why we need the fflush ( in ) please?
    Thanks

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    1)Thanks for your comments

    2) Does this code ( the one about characters ) with these changes run fine in your computer?
    Not in mine.

    3) When should i use EOF and when feof( stream ) ?

    4) I don't know if i have i hex editor, but, can you run that program and put an Enter after the number in the file and see the results and tell me what you think please?

    5) About the fflush you said "make sure it happens before another file reading function". Can you explain that a little bit more please?

    6) Sorry if i am getting tiring

    &)Thanks in advance.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    A last the code works!!
    Many thanks.

    But please, would you mind answering my question 3 ?

    Thanks again.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Ok, thanks, i got it.

    However, i just thought that we don't need the fflush(in) because the bufeer will be always empty since we get one-by one the characters and we don't use scanf(). But even if we used scanf we would use the %c so it would return fine without the need to read the next characcter.
    Am i right?

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    So, because i don't really understad the use of fflush(), when and after what statement ( after the printf()statement ?? ) do you recommend me to put it? ( If you could give me a link that explains very good how fflush() works and how to use it, i would be greatfull )

    Thanks again.
    Loading.....
    ( Trying to be a good C Programmer )

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>So, because i don't really understad the use of fflush(),
    I'll try and reword Salem's answer for you. When you write to a file, the data being written isn't sent directly to the file, instead it goes via a buffer which is out of your control. All you can tell is that the data is enroute to the file.

    As you want to read back from the same file, using the same FILE* variable, you *must* ensure that the writing of any data stored in the output buffer completes first. This is when the fflush() function comes in, as calling it will tell the OS to complete the write process.

    Here's the
    man page for fflush().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    8
    Try this, Notice the do while statement while scaning and printing into the file~


    #Include <stdio.h>



    int main()
    {
    int num;
    FILE *in;

    in=fopen("test.txt","r+");
    do
    {
    fscanf( in, "%d", &num );
    fprintf( in, "%d", num * 10 );
    }while (getchar(in))!=EOF);
    fclose( in );

    return 0;
    }

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    8
    try this,,notice that I have included the do ...while function, while reading and printing your file!

    #include <stdio.h>

    int main()
    {
    int num;
    FILE *in;

    in=fopen("test.txt","r+");
    do
    {
    fscanf( in, "%d", &num );
    fprintf( in, "%d", num * 10 );
    }while (getchar(in))!=EOF);
    fclose( in );

    return 0;
    }

  13. #13
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    That's completely wrong
    Loading.....
    ( Trying to be a good C Programmer )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 10:36 AM

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