- #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
#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