How to use fopen(), fread(), fwrite(), fprintf(), fscanf() properly?

Good day Sir/Ma'am,

Need your help on this one:
and i hope someone could [b]show me and make a sample code on how to do this... thanks a lot[/b]

If I have a [b]text file or notepad file[/b] like this one:

[b]Student.txt (with data like this:)[/b]
ST-0001, Eddie_VanHalen, 1
ST-0002, James_Hatfield, 2
ST-0003, Kurt_Cobain, 3
ST-0004, Marilyn_Manson, 4

[color=Green]How will I display these records on the screen if a user input its student number like this:[/color]

-----------------------------------------
Input Student No.: [b]ST-0001[/b]
-----------------------------------------

[color=Green]... then it will automatically display like this (or the OUTPUT):[/color]

-----------------------------------------
Student No.: [b]ST-0001[/b]
Student Name: [b]Eddie VanHalen[/b]
Student Level: [b]1[/b]
-----------------------------------------

[b]Thanks a lot Sir/Ma'am...[/b]

Comments

  • for information on how to use these functions look at [link=http://www.cplusplus.com/reference/clibrary/cstdio/]http://www.cplusplus.com/reference/clibrary/cstdio/[/link].

    there are also examples for every function.

    maybe you should firstly read the file and print everything out what you've read. in your next step you can modify this as you want to have it. for example if you read a line of your file you can split it and check either the current line's student no. equals with the one you put in or not.

    some functions of [link=http://www.cplusplus.com/reference/clibrary/cstdlib/]http://www.cplusplus.com/reference/clibrary/cstdlib/[/link] or [link=http://www.cplusplus.com/reference/clibrary/cstring/]http://www.cplusplus.com/reference/clibrary/cstring/[/link] should be helpful, too.

  • [code]
    #include
    #include
    void main()
    {
    FILE *fp;
    char str1, str2, str3;

    clrscr();

    if ((fp = fopen("tryme","r"))==NULL) {
    printf("Cannot open file
    ");
    }

    fscanf(fp,"%s %s %s",str1, str2, str3);
    fprintf(stdout, "%s %s %s",str1,str2,str3);
    fclose(fp);

    getch();
    }
    [/code]


    It doesn't display the text i want

    please help me....

    thanks in advanced
  • --------------------------------------------------------------------
    --------------------------------------------------------------------

    [B]Sir I think I got it working using this code [U]but I have one problem[/U]:
    this is the code:[/B]

    [CODE]
    #include
    #include
    #include
    #include
    void main()
    {
    FILE *fp;
    char code[80],code2[80], ename[80];
    int elevel;
    clrscr();
    if((fp = fopen("samplete.txt","r"))==NULL) {
    printf("cannot open file.
    ");
    }

    printf("Enter ID: ");
    gets(code2);

    while(!feof(fp)) {

    fscanf(fp,"%s %s %d",code,ename,&elevel);

    if(strcmp(code2,code)) {
    printf("%s %s %d",code,ename,elevel);
    break;
    }
    }
    fclose(fp);
    getch();
    }
    [/CODE]

    The problem is it only display the first record or line of text in my notepad file

    [B]lets say when I [COLOR="red"]Enter the ID: ST-0002[/COLOR][/B]

    it only display the first record or the first line of text file which is: [B]ST-0001, Eddie_VanHalen, 1[/B]

    it should be: [B]ST-0002, James_Hatfield, 2[/B]

    [B]Please help me... thanks in advanced[/B]
  • : [CODE]
    : #include
    : #include
    : #include
    : #include
    : void main()
    : {
    : FILE *fp;
    : char code[80],code2[80], ename[80];
    : int elevel;
    : clrscr();
    : if((fp = fopen("samplete.txt","r"))==NULL) {
    : printf("cannot open file.
    ");
    : }
    :
    : printf("Enter ID: ");
    : gets(code2);
    :
    : while(!feof(fp)) {
    :
    : fscanf(fp,"%s %s %d",code,ename,&elevel);
    :
    : if(strcmp(code2,code)) {
    : printf("%s %s %d",code,ename,elevel);
    : break;
    : }
    : }
    : fclose(fp);
    : getch();
    : }
    : [/CODE]

    hi,
    instead of:

    fscanf(fp,"%s %s %d",code,ename,&elevel);

    you should do

    fscanf(fp,"%[^,],%[^,],%d
    ",code,ename,&elevel);

    because if you use the %s format specifier, fscanf interprets the hole line as a string including commas. if you exclude the , you'll get what you want to. [edit: you also have to add your commas to the format string to determine your line.]

    the other problem that you get the wrong line is because of strcmp.
    strcmp returns:

    0 if both are equal
    something > 0 if the first argument is "greater" than the other one
    something < 0 otherwise

    a zero is interpreted in c/c++ as false and all other values as true. that's why your first line is always printed out.

  • [b]Thanks a lot sir... now I understand... [/b]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion