Thread: Use of fseek() function

  1. #1
    Registered User phoneix's Avatar
    Join Date
    Mar 2012
    Location
    India
    Posts
    7

    Use of fseek() function

    hey people
    i have written a program to demonstrate the use of fseek() function on a file.
    the program creates a file student.txt in writing mode and the user enters the name and roll number of four students which are stored on the file.
    after this the file is opened in reading mode and the user enters the record number which he needs to see.
    then using the fseek() function the program displays only that record of the file.
    the program is working fine but when i open the text file created, its contents are pretty messed up.
    here is my code
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    struct student
    {
    char name[30];
    char roll[12];
    }st;
    
    void main()
    {
    FILE *fp;
    int i=0;
    clrscr();
    fp=fopen("D:\student.txt","w");
    if(fp==NULL)
    {
    printf("\nError opening file...");
    exit(0);
    }
    for(i=0;i<=3;i++)
    {
    printf("\nenter name : ");
    scanf("%s",&st.name);
    printf("\nenter roll : ");
    scanf("%s",&st.roll);
    fwrite(&st,sizeof(struct student),1,fp);
    }
    fclose(fp);
    
    int x;
    printf("\nEnter record number : ");
    scanf("%d",&x);
    fp=fopen("D:\student.txt","r");
    if(fp==NULL)
    {
    printf("\nerror opening file...");
    exit(0);
    }
    fseek(fp,(x-1)*sizeof(struct student),SEEK_SET);
    if(fread(&st,sizeof(struct student),1,fp)==1)
    {
    printf("\nName = ");
    printf("%s\n",st.name);
    printf("Roll = ");
    printf("%s",st.roll);
    }
    fclose(fp);
    }
    My output is....
    Code:
    enter name : abhilash
    enter roll : 123
    enter name : arati
    enter roll : 456
    enter name : jayajeet
    enter roll : 789
    enter name : hyder
    enter roll : 135
    
    Enter record number : 3
    
    name= jayajeet
    roll= 789
    the program works fine with any number entered between 1 and 4.
    but when i open the file created in notepad its contents are all messed up.
    here is the content of the file student.txt
    Code:
    abhilash                      123         arati sh                      456         jayajeet                      789         hyder et                      135
    the entries made in the file have random letters in a weird pattern.
    i want to give it a traditional database look like the following one.
    Code:
    abhilash 123
    arati 456
    jayajeet 789
    hyder 135
    this i can do easily with fprintf() function but then the fseek() function does not work.
    this program is gonna be part of my project. any suggestion ???

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You cannot have both a nice legible datafile and the possibility to fseek in that file.
    The closest you can get is to initialize the stringfields with e.g ' ' characters before reading input using scanf().
    Kurt

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You only have one structure where you are storing data to prior to writing the information to the file. The first name/roll you enter gets stored in the st struct. You store an 8 character name into the name field which has space to store up to 30 characters. When you write that to the file you get all those 30 characters (plus the 12 for the roll field). When you go back and enter a new name/roll you are storing the data to the same place that held the first name/roll combination. Your second name entered is only 5 characters long (plus 1 extra for the null for a total of 6 characters). When you write out the st struct to your file the second time, it writes all existing 30 characters of the name. The first 6 of these characters have been overwritten with the new name, but the last two characters of the first name entered "sh" are still stored there... this is why you see "arati sh" in the name field.

    You would need to clear out the name/roll fields after each output to the file to prevent this from happening. This is what Zuk has mentioned doing, initializing the fields before each scan of the data into those fields.

    If you are writing the null characters stored when entering the name/roll fields (which you are doing with the fwrite call), then all that is "messed" up will be the file when you view it with notepad. When you read the data back to output to the screen, you read in the stored null as well. The printf calls know to stop output of string data when it reaches the null so your program's display would still look correct even if the file itself did not look correct.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User phoneix's Avatar
    Join Date
    Mar 2012
    Location
    India
    Posts
    7
    thank you so much for this info.
    helped a lot.

  5. #5
    Registered User phoneix's Avatar
    Join Date
    Mar 2012
    Location
    India
    Posts
    7
    Quote Originally Posted by hk_mp5kpdw View Post
    If you are writing the null characters stored when entering the name/roll fields (which you are doing with the fwrite call), then all that is "messed" up will be the file when you view it with notepad. When you read the data back to output to the screen, you read in the stored null as well. The printf calls know to stop output of string data when it reaches the null so your program's display would still look correct even if the file itself did not look correct.
    I was wondering if i can use fseek() function to delete a particular entry from the database. so far I've only been able to display a particular entry and in some other operations I need to delete a particular entry while keeping others intact.
    Any suggestion how i can do this??

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    Quote Originally Posted by phoneix View Post
    I was wondering if i can use fseek() function to delete a particular entry from the database. so far I've only been able to display a particular entry and in some other operations I need to delete a particular entry while keeping others intact.
    Any suggestion how i can do this??
    There are two ways to do this:

    1) Copy the file - record by record - and skip the one you want to delete.

    2) "fseek" to the record and mark it as "deleted", i.e. overwrite the name with some special value that seems appropriate. For example: "*****"

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    i think that process would be long what about using fseek to go to the location of the unwanted data and then cutting to a str and emptying the string with str = null may be i think it may be better...

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    Quote Originally Posted by Nyah Check View Post
    i think that process would be long what about using fseek to go to the location of the unwanted data and then " and emptying the string with str = null may be i think it may be better...
    In that case, the "null"-string would be what I called "some special value that seems appropriate". But keep in mind that the record size must not change. Otherwise your file structure is broken and "fseek()"ing will no longer work as you expect it. So you cannot really "cut" the record, e.g. make it occupy less memory by replacing it by a single '\0' value. You can overwrite it with up to sizeof(struct student) bytes instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to 'C' using fseek(), please help
    By leopardforest@g in forum C Programming
    Replies: 9
    Last Post: 05-09-2007, 04:53 PM
  2. Reg. fseek
    By pokks in forum C Programming
    Replies: 1
    Last Post: 01-16-2006, 01:28 PM
  3. Help With fseek();
    By Explicit in forum C Programming
    Replies: 3
    Last Post: 05-26-2004, 08:40 PM
  4. fseek
    By Max in forum C Programming
    Replies: 5
    Last Post: 12-15-2003, 03:21 PM
  5. fseek ???
    By davie_scotland in forum C Programming
    Replies: 2
    Last Post: 02-19-2002, 06:13 PM

Tags for this Thread