0

I'm making a simple c program to add, display and modify records for a student. It makes a .txt file, which accepts the records.

Long story short, I've managed to isolate which function is clearing my file and it's fgets. For some strange reason, fprintf works normally until I reach fgets which then proceeds to clear my file every time it's used. I've been racking my brain for 4 days because of this.

int main(int argc, char *argv[])
{
    int changes = 1; /* numbers of changes made to text file */
    FILE *f;
    f = fopen(argv[1], "wb+");
    fprintf(f, "this is a success");
    if (argc < 2) {
        printf("You dun goof'd.");
        exit(1);
    }
    if (f == NULL)
        exit(1);
    input_again(f, changes);
    error(f, changes);
    fclose(f);
    return 0;
}

void input_again(FILE *f, int quantity)
{
    char command[MAX];
    /*char cmd[25];
    char arg1[30];
    char arg2[30];
    char arg3[30];*/
    int changes = quantity;
    fprintf(f, "input again%d", changes);
    while ((fgets(command, MAX, stdin)) != NULL) { /* file gets cleared after input */
        printf("%s", command);
        sscanf(command, "%s %s %s %s", cmd, arg1, arg2, arg3);
        if (strlen(arg1) > 21 || strlen(arg2) > 21 || strlen(arg3) > 21)
            error(f, changes);
        if (strcmp(cmd, "add") == 0) {
            fprintf(f, "add");
            add(f, arg1, arg2, arg3, changes);
            printf("%d", changes);
        } else if (strcmp(cmd, "display") == 0) {
            display(f, arg1, changes);

        } else if (strcmp(command, "modify")== 0) {
            modify(f, arg1, changes);
        } else {
            error(f, changes);
        }*/
    }
}

I'm using cygwin to compile my program. If there's a simple problem with how I'm calling my code, please let me know.

  • How do you know? have you commented the while block? – Iharob Al Asimi Feb 20 '15 at 03:53
  • 1
    That won't even _compile_ since you've commented out some required variables. It also has an extraneous `*/` token. It's hard to debug code that's clearly bogus. In any case, I'd bet good money against it being a bug in `fgets` which millions of people have tested, as opposed to your own code which has been tested by, well, just you :-) – paxdiablo Feb 20 '15 at 03:59
  • Also, what are you trying to do? – Iharob Al Asimi Feb 20 '15 at 04:00
  • I bet it's `modify`, `add`, `display` or `error`, but not `fgets`... – Eugene Sh. Feb 20 '15 at 04:00
  • Reading and writing to the same file has [certain requirements](http://stackoverflow.com/questions/28228509/function-for-bmp-color-inverting-doesnt-work/28228841#28228841) – user3386109 Feb 20 '15 at 04:07
  • 3
    `wb+ ` will truncate the file content of the file if it is already available. Every time You run a program it will truncate your file. – Karthikeyan.R.S Feb 20 '15 at 04:17
  • Turns out I never actually closed the file which would of printed to my text file. I was stuck in an endless loop just like @EugeneSh. and paxdiablo suggested. Also closing with ctrl-c in cygwin did not help things at all which I really should of mentioned. Thanks for the suggestions guys. – TheThirdRekt Feb 21 '15 at 21:27

1 Answers1

-1

you are writing the file named on the command line argv[1], but do you mean to be reading stdin? That won't read what you just wrote.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Andras
  • 2,995
  • 11
  • 17
  • Turns out I never actually closed the file which would of printed to my text file. I was stuck in an endless loop just like @EugeneSh. and paxdiablo suggested. Also closing with ctrl-c in cygwin did not help things at all which I really should of mentioned. Thanks for the suggestions guys. – TheThirdRekt Feb 21 '15 at 21:31