Thread: fprintf and rewind help!

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    8

    Unhappy fprintf and rewind help!

    hi all,
    its my 1st time using fopen,fprintf and others and i cant seem to edit my file via fprintf!


    heres my program:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    char letter,check,nought,bleh,hi,a;
    int main()
    {
        printf("Hello world!\n");
        FILE*qwer;
        qwer=fopen("hi.txt","a+");
        if(qwer==NULL)
            {
                printf("error opening file.");
                fclose(qwer);
                return 0;
            }
    while(letter!=EOF)
    {
        letter=fgetc(qwer);
        if (letter=='\n' )
        {
            check=fgetc(qwer);
            if (check=='\n'&&letter=='\n'){
                    printf("\n\n");
            }
            else
            {
                    printf("%c%c",letter,check);
            }
    
    
        }
        else{
        printf("%c",letter);
    }
    }
    rewind(qwer);
    while(a==0){
            printf("u wanna put stuff into it?y/n");
            scanf("%c",&a);
            if(a=='y'||a=='Y')
            {
                printf("hi");
                fflush(stdin);
                scanf("%c",&bleh);
                fprintf(qwer,"%c",bleh);
            }
            else if (a=='N' || a=='n')
            {
               a=1;
            }
            else{}
    }
    printf("checking again");
    rewind(qwer);
    while(letter!=EOF && letter!=NULL)
    {
        letter=fgetc(qwer);
        if (letter=='\n' )
        {
            check=fgetc(qwer);
            if (check=='\n'&&letter=='\n'){
                    printf("\n\n");
            }
            else
            {
                    printf("%c%c",letter,check);
            }
    
    
        }
        else{
        printf("%c",letter);
    }
    }
    fclose(qwer);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    For starters, your code is poorly indented. Clear, consistent indentation and formatting is crucial to making code easy to read, write, maintain and understand.

    Second, you should compile at the maximum warning level (e.g. -Wall option for gcc):
    Code:
    gcc -Wall -ggdb3 -pedantic -std=gnu99 -O0 -o foo foo.c -lm -lpthread -lrtfoo.c: In function ‘main’:
    foo.c:54:32: warning: comparison between pointer and integer [enabled by default]
    NULL is a pointer constant. You probably meant to compare it to the null character that marks the end of a string in C. You should use '\0' instead. They both have a numeric value of 0, but they are of different types, and furthermore, using '\0' more accurately represents what you are doing.

    Third, you should avoid global variables. There is no reason to declare letter, check, etc as globals. Declare them in main. While you're at it, get rid of unused variables, they only clutter things up.

    Fourth, fgetc returns an int, not a char. That is so it can return all possible char values as well as special values like EOF. You should thus declare letter and check to be of type int.

    I'm not really clear on what you're trying to do here, but your logic seems broken either way. Are you trying to add characters to the end of a file that already has text? Insert characters at the beginning/middle of the file? Overwrite characters at the beginning/middle of the file? For one thing, your program prints a junk character when I run it, due to the fact that hi.txt doesn't exist, and the first time you try to read from it, fgetc returns EOF, which is assigned to letter, and then you try to print letter (which is EOF and doesn't print nicely).

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You also need to stop using fflush(stdin). Its behavior is undefined. On some platforms, it may do what you expect, but on other platforms, it could (theoretically) produce catastrophic results.

    Quote Originally Posted by C Standard 2011
    7.21.5.2 The fflush function
    Synopsis

    1
    Code:
    #include <stdio.h>
    int fflush(FILE *stream);
    Description
    2 If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can I rewind a string like I can a file?
    By Babkockdood in forum C Programming
    Replies: 2
    Last Post: 04-30-2012, 06:07 PM
  2. how to find coomon data in 3 files without rewind..
    By transgalactic2 in forum C Programming
    Replies: 31
    Last Post: 03-29-2009, 03:23 PM
  3. Replies: 7
    Last Post: 03-26-2008, 03:21 AM
  4. rewind(stdin); ? Right or Wrong?
    By Antigloss in forum C Programming
    Replies: 3
    Last Post: 10-26-2006, 12:53 PM
  5. forgot how to rewind in C++
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2002, 07:55 PM