Jump to content

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

- - - - -

  • Please log in to reply
13 replies to this topic

#1
madmhan84

madmhan84

    Newbie

  • Members
  • PipPip
  • 15 posts
Good day Sir/Ma'am,

Need your help on this one:

If I have a text file or notepad file like this one:

Student.txt (with data like this)

ST-0001, Eddie_VanHalen, 1
ST-0002, James_Hatfield, 2
ST-0003, Kurt_Cobain, 3
ST-0004, Marilyn_Manson, 4

How will I display these records on the screen if a user input its student number like this:

-----------------------------------------
Input Student No.: ST-0001
-----------------------------------------

... then it will automatically display like this (or the OUTPUT):

-----------------------------------------
Student No.: ST-0001
Student Name: Eddie VanHalen
Student Level: 1
-----------------------------------------

Thanks a lot Sir/Ma'am...

Edited by madmhan84, 03 May 2011 - 04:03 PM.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You need to create a struct (structure) first to hold student's data, you may wish to use a linked list.

To open the students data file you need to first create a data structure:
FILE* infile;
Next you can open a handle to the file with fopen and specify a mode:
infile = fopen("students.txt", "r"); //r = read, w = write, rw = read write
Now you can use one of the fread, fgets, or fscanf functions in a loop to read the whole file extracting data in to your list of students.

Function references:
fscanf - C++ Reference
fgets - C++ Reference
fread - C++ Reference
feof - C++ Reference

Once you have completed that, you will ask the user to input a student record in any way you wish, and search your linked list of student records for the one matching the ID you give, and output the results.

Can you try to create an example program with the information you have been provided, and if run in to problems show us your code?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
madmhan84

madmhan84

    Newbie

  • Members
  • PipPip
  • 15 posts

#include<stdio.h>

#include<conio.h>

void main()

{

FILE *fp;

char str1, str2, str3;


clrscr();


if ((fp = fopen("tryme","r"))==NULL) {

printf("Cannot open file\n");

}


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

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

fclose(fp);


getch();

}


It doesn't display the text i want :confused:

please help me....

thanks in advanced

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
You have declared str1, str2 and str3 as a single character only but you're using them as strings. Also, you seem to be have comma as delimiter (separator) in your text file, so you should modify fscanf format:

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


A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
madmhan84

madmhan84

    Newbie

  • Members
  • PipPip
  • 15 posts

Flying Dutchman said:

You have declared str1, str2 and str3 as a single character only but you're using them as strings.
Sir how can i declare it as not a single character?
is this right?
str1[80], str2[80], str3[80]

Flying Dutchman said:

Also, you seem to be have comma as delimiter (separator) in your text file, so you should modify fscanf format:

how will i do this? can you give me a sample sir... thanks in advanced

#6
madmhan84

madmhan84

    Newbie

  • Members
  • PipPip
  • 15 posts
Sir I think I got it working using this code but I have one problem:
this is the code:



#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

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.\n");

}


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();

}


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

lets say when I Enter the ID: ST-0002

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

it should be: ST-0002, James_Hatfield, 2

Please help me... thanks in advanced

#7
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Try
fscanf(fp,"%s,%s,%d",code,ename,&elevel);
as suggested by Flying Dutchman. Also, strcmp() returns a zero when the strings are equal. Try
if(!strcmp(code2,code))
instead of the
if(strcmp(code2,code))
you have.

EDIT: Actually, it doesn't seem to work with the commas in the fscanf(). The best thing I could come up with is quite hacktastic, but it works:

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>


void main()

{

FILE *fp;

char read[80],code[80],code2[80], ename[80];

int elevel;

if((fp = fopen("samplete.txt","r"))==NULL) {

printf("cannot open file.\n");

}


printf("Enter ID: ");

gets(code2);

code2[7]=',';

code2[8]=0;


 while(!feof(fp)) {


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


        if(!strcmp(code2,code)) {

           printf("%s %s %i",code,ename,elevel);

           break;

           }

}

fclose(fp);

getch();

}

Of course, it only works with the specific "ST-####" strings. To make it more flexible, you could use an strlen() and use the length to put the comma and null in a certain place.
Latinamne loqueris?

#8
madmhan84

madmhan84

    Newbie

  • Members
  • PipPip
  • 15 posts
Sir mebob,


code2[7]=',';

code2[8]=0;

Sir thanks a lot, but what is the use of this code above that you add? (Im curious and I want to learn more, thanks again)


also this one:

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

printf("%s %s %i",code,ename,elevel);

why did you change %d to %i?

thanks for your kind reply sir

#9
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
%d is the same as %i.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#10
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
%d can stand for decimal, or base 10 (including base 10 fractions). %i simply denotes integer, which is the explicit type you are using regardless of it being a decimal or not. They are equivalent in the terms of parameters.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#11
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
I added the comma to the user entered string because I can't seem to get the comma out of the name extracted from the file. It always comes out as "ST-0001," or the likes. That string and "ST-0001", the user entered one, will never equal each other. So what I did is add a comma to the user entered string, so they can compare and be equal. It isn't really a dependable solution, just a temporary one. And I changed %d to %i because, being the idiot I am, I was thinking that %d was a double lol.
Latinamne loqueris?

#12
madmhan84

madmhan84

    Newbie

  • Members
  • PipPip
  • 15 posts

mebob said:

It isn't really a dependable solution, just a temporary one.

Thanks Sir, but what will be the dependable solution? can you teach or show me how? TIA sir



mebob said:

And I changed %d to %i because, being the idiot I am, I was thinking that %d was a double lol.
hahahaha... but thanks to you again sir


@ALL who reply on this thread..... thank you very much




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users