Jump to content

Help needed with fscanf( )

- - - - -

  • Please log in to reply
4 replies to this topic

#1
DeJMAn

DeJMAn

    Newbie

  • Members
  • Pip
  • 2 posts
Hi,
Im working on a menu-driven(switch statement) program to:

1. Enter a student record into a file
2. Search for a student record in a file
3. Delete a record from a file

A single file "Student.txt" is used.
I managed to figure out how to enter the record to the file using fprintf, but now, im stuck at searching from the file which i have no idea on how to do.
The file will contain Roll number(integer) , Student Name(string of 20) , Address( which is a single world like Miami or Houston), Marks obtained(integer) , Status ( character "P" or "F").

Any help on this would be greatly appreciated.

#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
Need more details, such as some code. But generally if you want to search the file for a given record you just read the file serially -- from beginning to end -- until you find the record you want. Most likely you will prompt for the student's roll number, something along these lines, assuming the file was written in binary mode and not text mode (binary mode is the simplest way to do that program)

int rollnumber;

printf( "Enter student Rolll Number\n");

fscanf("%d",&rollnumber);

FILE* in = fopen("student.txt", "rb");

while( fread((char *)&record, 1,sizeof(record) , in)

{

   if( record.rollnumber == rollnumber)

   {

      // got it!

      break;

   }

}


Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3
DeJMAn

DeJMAn

    Newbie

  • Members
  • Pip
  • 2 posts
while( fread((char *)&record, 1,sizeof(record) , in)

could you please explain that line...i think i got it....

#4
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
fscanf() reads the text of a file up to the next whitespace character and interprets it as a given data type. It works the same way as scanf(), only it works for general file streams, not just standard input.

And to clarify, fscanf() works for text files. fread() works for binary files. (Don't ask me how it works. I've never actually used it.)
Programming is a journey, not a destination.

#5
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
fread() goes along with fwrite(), which read/write to binary formatted files. A binary file is one in which it contains the data as it is represented internally in your program's memory. So if your program has an integer whose value is 123, fwrite() writes the internal representation of that value, not the readable digits '1', '2', and '3' as you would see when using fprintf().

Its a great deal faster and far easier (meaning a lot less coding) to read/write binary files that contain structures than it is to do it in text mode were each member of the structure has to be read/written. Structures can be written and read all in one simple shot as in the example I posted previously. The down side to this is that you can't use other programs such as a Notepad.exe to read binary files.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users