i am new on file(c)
if i copy
fscanf(nom du file,"%d",&a);
from where i want copy th value of a

plz send for me a example use fscanf plzzzzzzzz realy i need help :cry: :cry:

Recommended Answers

All 2 Replies

i am new on file(c)
if i copy
fscanf(nom du file,"%d",&a);
from where i want copy th value of a

plz send for me a example use fscanf plzzzzzzzz realy i need help :cry: :cry:

let me give you an example, Now if you have a file opened in textmode, which has integer values stored in it, and you want to read these numbers through C-code, then you can use fscanf();.
Now let the file name is : NUM.txt
then the code given will read nos. from this file.

#include<stdio.h>
#include<conio.h>
main()
{

FILE *fp; //File pointer
int num; //num variable will collect integers from the file
fp=fopen("NUM.txt","r");
if(fp==NULL) //if file failed to open
{
printf("\nFile not opened\nPress any key to exit");
getch();
exit(0);
}
char ch;
while(ch!='e'||ch!='E') //while you not press 'E'
{
fscanf(fp,"%d",&num);
printf("%d\n",num);
ch=getch();
}
}

If you meet any error plz inform me.

>If you meet any error plz inform me.
Okay.

>#include<conio.h>
Nonstandard header, typically only Windows or DOS compilers will have it. But AFAIK Borland compilers are the only ones that have a fully featured conio.h. Using it is dicey at best because the functions from it that you use may or may not be on another compiler that has conio.h, much less those that do not.

>main()
Be explicit as C will refuse to accept this in the future:

int main ( void )

>getch();
Nonstandard function, use at your own risk. But getchar works just as well. Of course the counter argument is that getchar doesn't return until a newline (or error) is encountered. But really, how much is it to ask a user to type return rather than any key?

>exit(0);
Why are you returning 0 if the program is terminating on an error? 0 means success, so you would be better off including stdlib.h and returning EXIT_FAILURE.

>char ch;
This tells me you're compiling as C++. Declarations can only be placed at the beginning of a block in C89. C99 changed this, but because you're using conio.h I feel reasonably comfortable calling you on the error as I don't know of any compilers that conform to C99 and support conio.h.

>while(ch!='e'||ch!='E')
&& would bemore appropriate than ||.

>}
>}
main returns an int, so return an int or suffer undefined behavior.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.