Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

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

scanf fopen

  • Please log in to reply
13 replies to this topic

#1 madmhan84

madmhan84

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 05 April 2011 - 11:49 PM

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.

  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 06 April 2011 - 01:34 AM

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?
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 madmhan84

madmhan84

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 01 May 2011 - 04:49 PM

#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
  • 0

#4 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 01 May 2011 - 04:58 PM

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

  • 0

The roots of education are bitter, but the fruit is sweet.


#5 madmhan84

madmhan84

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 01 May 2011 - 11:42 PM

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]

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
  • 0

#6 madmhan84

madmhan84

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 03 May 2011 - 03:58 PM

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
  • 0

#7 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 03 May 2011 - 04:39 PM

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.
  • 0
Latinamne loqueris?

#8 madmhan84

madmhan84

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 03 May 2011 - 05:40 PM

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
  • 0

#9 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 04 May 2011 - 04:06 AM

%d is the same as %i.
  • 0

The roots of education are bitter, but the fruit is sweet.


#10 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 04 May 2011 - 05:06 AM

%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.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#11 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 04 May 2011 - 06:32 AM

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.
  • 0
Latinamne loqueris?

#12 madmhan84

madmhan84

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 04 May 2011 - 04:35 PM

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



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
  • 0





Also tagged with one or more of these keywords: scanf, fopen

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download