Solving Simple C Problem with Text Input/Output Files

  • Thread starter Thread starter Vectorspace
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around troubleshooting a C program that handles text input and output files. Participants are examining issues related to file handling, syntax errors, and expected versus actual outputs.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares their code and describes an issue with opening the input file, receiving an error message.
  • Another participant suggests checking the file path and removing semicolons after the if statements, indicating that these semicolons create unintended anonymous blocks.
  • There is a recommendation to change the return type of the main function from void to int and to include a return statement at the end of main.
  • Concerns are raised about the use of a pragma directive to disable warnings, with a suggestion that this practice could lead to future issues.
  • One participant points out that the same FILE pointer is being reused incorrectly for different files, suggesting that a different pointer should be used for opening the GPA file.

Areas of Agreement / Disagreement

Participants generally agree on the issues with semicolons and the use of FILE pointers, but the discussion remains unresolved regarding the specific file path issue and the implications of using void main.

Contextual Notes

There are unresolved assumptions about the correct file paths and the environment in which the code is being executed. The discussion also highlights potential misunderstandings about C programming conventions.

Vectorspace
Messages
17
Reaction score
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
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
 
What do you expect the output to be, and what do you actually get?
 
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.
 
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...
 
What do you mean the "real" location?
 
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);
 
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.
 
got it! Thanks.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
11K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
3
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K