Solving Simple C Problem with Text Input/Output Files

  • Thread starter Vectorspace
  • Start date
In summary, you are trying to open a text file in Windows using a FILE pointer. You are getting an error because the semicolons are missing after the if statements.
  • #1
Vectorspace
17
0
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?

#include<stdio.h>
#include<stdlib.h>
#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");
exit(1);
}
if((namefp=fopen("c:\\Users\\Steve\\names.txt","w"))== NULL);
{
printf("unable to open names file\n");
exit(2);
}

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

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

fclose(fp);
fclose(namefp);
fclose(gpafp);

} // end of main
 
Technology news on Phys.org
  • #2
Here is what is contained in the file studinfo:
Albert Einstein 2.3
Salvadore Dali 3.1
Neamiah Scudder 1.4Here is what is contained in the file names:
Einstein, Albert
Dali, Salvadore
Scudder, NeamiahAnd here is what is in the file gpa:
2.33
3.13
1.44
 
  • #3
What do you expect the output to be, and what do you actually get?
 
  • #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.
 
  • #5
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...
 
  • #6
What do you mean the "real" location?
 
  • #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);
 
  • #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.
 
  • #9
got it! Thanks.
 

1. How do I read input from a text file in C?

In C, you can use the fscanf() function to read input from a text file. This function takes in the file pointer and a format specifier as parameters, and assigns the values from the file to the specified variables.

2. How do I write output to a text file in C?

To write output to a text file in C, you can use the fprintf() function. This function takes in the file pointer and a format specifier as parameters, and writes the specified values to the file.

3. How can I check if a file exists in C?

In C, you can use the fopen() function to open a file. If the file does not exist, this function will return NULL. Therefore, you can check if the file exists by checking if the return value of fopen() is equal to NULL.

4. How do I handle errors when working with text files in C?

You can use the feof() function to check if the end of the file has been reached, and the ferror() function to check if any errors have occurred while reading or writing to the file. Additionally, you can use the perror() function to print out the specific error message.

5. Can I use text files to store and read data structures in C?

Yes, you can use text files to store and read data structures in C. However, you will need to carefully format the data in the file and use proper functions to read and write the data. Alternatively, you can use binary files which are more efficient for storing and retrieving data structures.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
11K
Replies
10
Views
1K
Back
Top