Like Tree2Likes
  • 1 Post By oogabooga
  • 1 Post By Pheetuz

FSCANF/FPRINTF Skip Line and Print

This is a discussion on FSCANF/FPRINTF Skip Line and Print within the C Programming forums, part of the General Programming Boards category; Hello. I'm trying to skip line and input text which will overwrite the current one in .txt file. I've tried ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    5

    FSCANF/FPRINTF Skip Line and Print

    Hello.

    I'm trying to skip line and input text which will overwrite the current one in .txt file.

    I've tried this:

    Code:
    int main()
    {
    	FILE *txt;
    	
    	txt = fopen("Something.txt", "r+t");
    	
    	fscanf(txt,"%*[^\n]%*c");
    	fprintf(txt,"Something");
    	system("PAUSE");
    }
    Oddly enough it doesn't work.

    Here's the contents of Something.txt before running the program:

    Code:
    0123456789
    0123456789
    And after running the program it stays exactly the same.

    I want it to output Something.txt like this:

    Code:
    0123456789
    Something9
    I've used Google and forums of this site but couldn't find an answer that would satisfy my needs.
    Last edited by Ditrik; 01-08-2012 at 11:12 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Are you looking in the file whilst stuck at system("PAUSE");

    Because until you either flush the stream, close the file, or end the program, not a lot is likely to happen to the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    5
    Each time I've tried this I've ended the program so the changes should happen.

    I've fiddled around it little more and found out that after adding fseek(txt, 0L*sizeof(char), SEEK_CUR) between fscanf and fprintf it works.
    But I'm having trouble explaining why it works now and why it doesn't work without that line.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I believe that when you open a file in mixed mode you must execute a file-positioning function before switching from reading to writing or vice versa.
    Salem likes this.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Seems to work OK for me, using gcc/Linux.
    Code:
    $ cat bar.c
    #include <stdio.h>
    int main()
    {
        FILE *txt;
    
        txt = fopen("Something.txt", "r+");
    
        fscanf(txt,"%*[^\n]%*c");
        fprintf(txt,"Something");
        fclose(txt);
        return 0;
    }
    $ gcc bar.c
    $ cat Something.txt 
    0123456789
    0123456789
    $ ./a.out 
    $ cat Something.txt 
    0123456789
    Something9
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Probably a Windows thing then, it even seems to need the "repositioning" in binary mode. Could someone quote the standard on this?

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    12
    As I discovered myself today you need to either use the seek command between file reads and writes or the recommended method for avoiding this problem is to only read / write from a file between each open. So only open a file, read from it and then close it, then re-open it, write to it, close it.

    Otherwise the pointer value in the FILE struct is undefined.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    12
    Note the bit in bold

    Taken from another post:
    fopen function

    Purpose:
    Opens a stream. The safer fopen_s function is also available.

    Syntax:
    FILE * fopen(const char *name, const char *mode);

    Declared in:
    <stdio.h>


    The fopen function opens the file whose name is the string pointed to by name, and associates a stream with it. The string may contain a full path (from the root), a relative path (from the current directory) or just a name.

    The argument mode points to a string. If the string is one of the following, the file is open in the indicated mode.

    Mode Description
    "r" Open text file for reading.
    "w" Truncate to zero length or create text file for writing.
    "a" Append; open or create text file for writing at end-of-file.
    "rb" Open binary file for reading.
    "wb" Truncate to zero length or create binary file for writing.
    "ab" Append; open or create binary file for writing at end-of-file.
    "r+" Open text file for update (reading and writing).
    "w+" Truncate to zero length or create text file for update.
    "a+" Append; open or create text file for update, writing at end-of-file.
    "rb+" Open binary file for update (reading and writing).
    "wb+" Truncate to zero length or create binary file for update.
    "ab+" Append; open or create binary file for update, writing at end-of-file.
    "r+b" Same as "rb+"
    "w+b" Same as "wb+"
    "a+b" Same as "ab+"

    Opening a file with read mode ('r' as the first character in the mode argument) fails if the file does not exist or cannot be read.

    Opening a file with append mode ('a' as the first character in the mode argument) causes all subsequent writes to the file to be forced to the then current end-of-file, regardless of intervening calls to the fseek function.

    When a file is opened with update mode ('+' as the second or third character in the mode argument), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file.

    When opened, a stream is fully buffered if and only of it can be determined not to refer to an interactive device. The error and end-of-file indicators for the stream are cleared.

    Returns:
    A pointer to the object controlling the stream on success, otherwise a null pointer.
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using 'For' with fprintf/fscanf
    By Pecado in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2010, 07:38 AM
  2. Skip line in file - fscanf
    By Noori88 in forum C Programming
    Replies: 2
    Last Post: 05-27-2007, 07:57 AM
  3. How to write in a file using fscanf and fprintf
    By g_p in forum C Programming
    Replies: 8
    Last Post: 05-24-2007, 02:52 PM
  4. Help about fopen,fscanf,fprintf
    By bluexrogue in forum C Programming
    Replies: 1
    Last Post: 09-03-2001, 09:05 PM
  5. fprintf help ,my print outs wont line up
    By plivermo in forum C Programming
    Replies: 2
    Last Post: 08-15-2001, 10:39 AM

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