-
Reading from a text file
I have a text file vidi1.txt. All this file has is a single number. I have written a C program to read this file & display its contents. This does not give results as expected. Please help.
Code:
#include<stdio.h>
void main()
{
FILE *f1;
int i;
clrscr();
f1=fopen("c:\tcc\vidi1.txt","rb");
printf("Contents of file\n");
i=getw(f1);
fclose(f1);
printf("%d",i);
getch();
}
-
int main(void) - read FAQ
backslash should be escaped:
"c:\\tcc\\vidi1.txt"
-
> i=getw(f1);
I'm not sure what getw() does, but you can do the same thing with fscanf():
Code:
(if fscanf(f1, "%d", &i) == 1)
{
printf("%d", i);
}