I want to open a file in C:\text.txt and print it.
But, it doesn't work. What's wrong in the below statement?
thx!
#include <stdio.h>
void main()
{
int a,b;
b=fopen("c:\test.txt");
fscanf(b,"%d\n",&a);
printf("%d",a);
getchar();
}
I want to open a file in C:\text.txt and print it.
But, it doesn't work. What's wrong in the below statement?
thx!
#include <stdio.h>
void main()
{
int a,b;
b=fopen("c:\test.txt");
fscanf(b,"%d\n",&a);
printf("%d",a);
getchar();
}
You have the syntax of fopen all wrong. fopen returns a FILE * to the file you opened. The first parameter of fopen is the file, and second parameter (2 parameters in all) is the order of what you want to do ("r" -- read, "w" -- write). The syntax is:
<FILE * type> = fopen(filename, "r" or "w");
If you are working with binary files, you just add "b" with the "r" or "w". Your code, revised, would be:
Understand???Code:#include <stdio.h> void main() { int a; FILE *fp; fp=fopen("c:\test.txt", "r"); fscanf(fp,"%d",&a); printf("%d",a); getchar(); }
--Garfield
1978 Silver Anniversary Corvette
thanks for reply !
i am using Visual Studio 6 and win 98 to run the program .
but it says Expression: str != NULL
can't find FSCANF.C.
However, I can't find FSCANF.C in my computer.
Post all of your code.
1978 Silver Anniversary Corvette
fscanf is a function in the stdio.h header library.
1978 Silver Anniversary Corvette
Ummm...bad link. Just post the code. What is the bmp?
1978 Silver Anniversary Corvette
also... watch out for the backslash... the compiler will think that's an escape sequence, so you need to use a double backslash to get it...
hasafraggin shizigishin oppashigger...
Just the code which u told me.Originally posted by Garfield
Ummm...bad link. Just post the code. What is the bmp?
Code:#include <stdio.h> void main() { int a; FILE *fp; fp=fopen("c:\test.txt", "r"); fscanf(fp,"%d",&a); printf("%d",a); getchar(); }
As DA said, you have to use \\
Code:fp=fopen("c:\\test.txt", "r");
I should use this method??
#include <stdio.h>
void main()
{
char a[200];
FILE *fp;
fp=fopen("c:\\test.txt", "r");
fscanf(fp,"%s",&a);
printf("%s",a);
getchar();
}
but, how can I read all the texts in a txt file since there is a limit for array?
And I just can read line 1 only.
Fixed code:
#include <stdio.h>
void main()
{
char a[100];
FILE *fp;
fp=fopen("c:\\test.txt", "r");
while (fscanf(fp,"%c",&a) != EOF)
{
printf("%c",a);
}
getchar();
fclose(fp);
}
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
thx, but it appears some strange characters.Originally posted by GaPe
Fixed code:
#include <stdio.h>
void main()
{
char a[100];
FILE *fp;
fp=fopen("c:\\test.txt", "r");
while (fscanf(fp,"%c",&a) != EOF)
{
printf("%c",a);
}
getchar();
fclose(fp);
}
I did something like this:
It can read the texts from a .txt file.Code:#include <stdio.h> void main() { char a[500]; FILE *fp; fp=fopen("c:\\test.txt", "r"); while (fscanf(fp,"%s",&a) != EOF) { printf("%s",a); } getchar(); fclose(fp); }
However, it prints all the texts in 1 line.