reading and writing to a binary file

This is a discussion on reading and writing to a binary file within the C Programming forums, part of the General Programming Boards category; Hey people, i've followed the tutorial on how to do file I/O to binary files however i need one more ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    23

    reading and writing to a binary file

    Hey people, i've followed the tutorial on how to do file I/O to binary files however i need one more line, but im not sure what to type in

    its supposed to open the binary file binFile for reading and writing. then w ite each student record into binFile and therefore read the specified student record from the binary file into the temporary variable tempStudent.

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    struct student
    {
      char name[200];
      float marks;
    };
    
    int main()
    {
    
      FILE * txtFile;
      struct student tempStudent;
      // File pointer to binary file
      FILE * binFile;
      int searchNum;
    
      if ((txtFile = fopen("input.txt", "r")) == NULL)
      {
        printf("Can not open file input.txt\n");
      }
    
      else
      {
         binFile = fopen("binary_file.txt", "wb");
      }
    
    
      while (fscanf(txtFile,"%s %f", tempStudent.name, &(tempStudent.marks)) == 2)
      {
       // write code that stores the structure tempStudent into the binary file
      }
    
        printf("Please enter the student you want to search for\n");
        printf("For example if you want the first student type 1\n");
        scanf("%d", &searchNum);
    
        fseek(binFile, searchNum*sizeof(tempStudent), SEEK_SET);
        fread(???, sizeof(tempStudent), 1, binFile);
    
        printf("The student name retreived is: %s\n", tempStudent.name);
        printf("The student mark retreived is: %.2f\n", tempStudent.marks);
        fclose(binFile);
        fclose(txtFile);
    
      return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You found fread but didn't see fwrite?

    Also tyring to read from a FILE pointer opened for writing isn't the best idea in the world. Wouldn't hurt to reopen the file for reading, which will also rewind the file.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    yeah i suppose, just not sure this might work, for the binary file that's another program, i'll create so it just opens the binary file to display the information.

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    struct student
    {
      char name[200];
      float marks;
    };
    
    int main()
    {
    
      FILE * txtFile;
      struct student tempStudent;
      // File pointer to binary file
      FILE * binFile;
      int searchNum;
      char s[200];
    
      if ((txtFile = fopen("input.txt", "r")) == NULL)
      {
        printf("Can not open file input.txt\n");
      }
    
      else
      {
         binFile = fopen("binary_file.txt", "wb");
      }
    
    
      while (fscanf(txtFile,"%s %f", tempStudent.name, &(tempStudent.marks)) == 2)
      {
       // write code that stores the structure tempStudent into the binary file
      }
    
        printf("Please enter the student you want to search for\n");
        printf("For example if you want the first student type 1\n");
        scanf("%d", &searchNum);
    
        fseek(binFile, searchNum*sizeof(tempStudent), SEEK_SET);
        fread(name, sizeof(tempStudent), 200, binFile);
        fwrite(name, sizeof(tempStudent), 200, binFile);
    
        printf("The student name retreived is: %s\n", tempStudent.name);
        printf("The student mark retreived is: %.2f\n", tempStudent.marks);
        fclose(binFile);
        fclose(txtFile);
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems installing Mingw5.1.4
    By BlackOps in forum C Programming
    Replies: 2
    Last Post: 07-26-2009, 03:28 AM
  2. Binary Tree - Reading From and Writing to a File
    By Ctank02 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2008, 09:22 PM
  3. reading and writing to the end of a binary file
    By cloudy in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2005, 04:03 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21