Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simple C Problem

  1. Apr 10, 2008 #1
    Hey guys. I am trying to figure out how to use text input/output files in c. Here is the code that I have written, would someone please tell me what I am doing wrong?

    #pragma warning(disable:4996)

    void main()
    FILE *fp,*namefp, *gpafp;
    char first[15], last[15], inbuf[80];
    double gpa;
    if((fp=fopen("c:\\Users\\Steve\\studinfo.txt","r")) == NULL)
    printf("Unable to open input file\n");
    if((namefp=fopen("c:\\Users\\Steve\\names.txt","w"))== NULL);
    printf("unable to open names file\n");

    if((namefp=fopen("c:\\Users\\Steve\\gpas.txt","w")) == NULL);
    printf("unable to open gpa file\n");

    while(fgets(inbuf, 80, fp) != NULL)
    sscanf(inbuf,"%s %s %f%*c", first, last, &gpa);
    fprintf(namefp,"%s, %s\n", last,first);
    } // end of while


    } // end of main
  2. jcsd
  3. Apr 10, 2008 #2
    Here is what is contained in the file studinfo:
    Albert Einstein 2.3
    Salvadore Dali 3.1
    Neamiah Scudder 1.4

    Here is what is contained in the file names:
    Einstein, Albert
    Dali, Salvadore
    Scudder, Neamiah

    And here is what is in the file gpa:
  4. Apr 10, 2008 #3


    User Avatar

    Staff: Mentor

    What do you expect the output to be, and what do you actually get?
  5. Apr 10, 2008 #4
    The output should simply print out the data from those files. I am getting "Unable to open input file". So the first if statement is TRUE.
  6. Apr 11, 2008 #5

    jim mcnamara

    User Avatar

    Staff: Mentor

    You are on Windows -
    go into windows explorer and find the real location and name of the "studinfo.txt" file you are trying to open. LOSE the semicolons after the open statements and maybe it will work. As you code is written it will always exit in one of those accidental anonymous blocks you create by leaving the semicolons.

    ps: int main( ) returns an int - add a last line in main: return 0; Don't use void main().

    And what does that pragma do? disable warnings? Don't do that.... If you compile an run a program with warnings and it works, you are asking for trouble later on - it's called programming by coincidence and it will always come back to haunt you. I'll bet it doesn't like void main to start with....
  7. Apr 11, 2008 #6
    What do you mean the "real" location?
  8. Apr 12, 2008 #7
    Can anyone help me?
    Those semi-colons after the if statements shouldn't be there. i.e. if((namefp=fopen("c:\\Users\\Steve\\gpas.txt","w") ) == NULL);
  9. Apr 14, 2008 #8
    Indeed, the ; should not be there... what happens if you remove them?

    By the way, you are using namefp as a FILE pointer twice; you should be using gpafp when trying to open the file gpas.txt.
  10. Apr 15, 2008 #9
    got it! Thanks.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook