LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2006, 01:53 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
C programming fread and fwrite using structures


Acording to the man page and what information that I can gather fread and fwrite work with arrays. Im using them to write data to a file in structures and then using fread to retrive the data using fseek to locate it. Im having a bit of an issue getting the data back. So is it not possible to use this to write structure data or am i not understanding how these fuctions work ?

here is the writeing part
Code:
int addcd(DATANODE *infoadd,const char *database)
{
    FILE *cddat;
     int number;

    if ((cddat = fopen(database,"a")) == NULL)
      {
       printf("\nError writeing to file!. func addcd\n");
       return 1;
      }

    
    
    fwrite(infoadd,sizeof(DATANODE),1,cddat);
    rewind(cddat); 

    if (fclose(cddat))
             { 
        printf("\nError closeing file. func addcd\n"); 
        return 1; 
      }
  
  else 
    return 0; 
}
Please be aware this isent my most current write that im using its an older one so syntax may be wierd or fixed alredy but this gives you an idea of how im writeing data to the drive. According to what i know about how writeing to a file works this should write the data to the disk like this
Quote:
[block of data][block of data] EOF
but its not no matter how much data i put on the file all i ever get is the first thing ever written to it.
My listing file checks for the disk number in DATANODE and keeps going though each block untill it gets to EOF but it seams to check the first one then stop like its gotten to eof. From what i can gather EOF is not being moved when a file write is being preformed so im getting something like this.

Quote:
[block of data]EOF[new block]
can someone clear this up and if you are intrested I can send you a copy of all the files im working with.


here is an older one i worked with but ive since rewritten it so it may not be usefull
Code:
 
 #include"cddat.h"


int listfile(const char *basename)
{
   FILE *basefile; 
   DATANODE *temphold; 
   int discnum;
   int check; 
   int ditype; 
   
   if ((basefile = fopen(basename,"r")) != NULL)
 { 
   

   temphold = malloc(sizeof(DATANODE));
   
    

  do {
   printf("\nWhat disc would you like to access?\n"); 
   printf("DISK#="); 
   scanf("%d",&discnum); 
   fseek(basefile,discnum,SEEK_SET);
   check = fread(temphold,sizeof(DATANODE),1,basefile);
   rewind(basefile); 
   if (check > 0)
        { 
          printf("\nDisk number: %d\n",temphold->cdnum); 
          printf("\nDisk name: %s",temphold->cdname); 
          printf("\nType: "); 
          ditype = temphold->disktype;
          switch(ditype)
           {
            case 1: printf("CD"); 
            break; 
            case 2: printf("DVD"); 
            break; 
            case 3: printf("Data CD"); 
            break; 
            case 4: printf("Data DVD"); 
            break;
            default: printf("%d",ditype); 
            break; 
            }
          printf("\nCondition:"); 
          if (temphold->condition == 1)
            printf("BAD");
          else 
            printf("GOOD"); 
          
          printf("\nNote at this time listfiles are disabled\n");
          check = 1; 
         }
    else 
        {
         check = 0; 
         printf("\nDisc not found please enter a valid disc name!\n"); 
        }
  }while (check != 1); 
 
  free(temphold); 
  fclose(basefile); 
 }
else 
  { 
    printf("\nError opening file (this is bad)\n"); 
    return 1; 
  } 
 
return 0; 
}
P.S This is my last ditch effort to try and get this code to work if I cant then im going to rewite the way that im saving data so that im using a text file instead of a binary file.

Last edited by exvor; 09-25-2006 at 02:08 PM.
 
Old 09-25-2006, 03:29 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
1. rewind() moves to the beginning of file, that's why you don't have more blocks
2. I don't understand the problem with EOF, could you show a simple example with data?
 
Old 09-25-2006, 03:34 PM   #3
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
It is certainly possible to write/read structures (or anything else) to/from a file with fwrite/fread.

Perhaps the following line

Quote:
Code:
fseek(basefile,discnum,SEEK_SET);
should be:

Code:
fseek(basefile,discnum*sizeof(DATANODE),SEEK_SET);
since the offset is in bytes.

Since you rewind() after reading, there isn't any problem there except that it'ss unnecessary, since SEEK_SET is relative to the beginning of the file, not the current position (that's SEEK_CUR). The rewind() in the writing code is also unnecessary, since you just close the file immediately after.

EDIT: (The memory leak that I thought was there is not. The weird indentation just made it look like there was one.)

Last edited by zhangmaike; 09-25-2006 at 03:45 PM.
 
Old 09-25-2006, 04:15 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Everything that you pointed out has been fixed and refixed to try and reslove I really do wish i had an update version to show you as this is old and i found those issues before.

basicly whats going on is this

there are 2 recods in the file

lets say record one is test1 and record 2 is test2

i have a loop that gets data from the file in the form of
temphold is a pointer to a structure that i used malloc to allocate.

fread(temphold,sizeof(DATANODE),1,basefile);

by the way the seek was like this
fseek(basefile,(sizeof(DATANODE) * looper),SEEK_SET);

looper starts at 0 and increments up from there.

it then checks the value of temphold->discnum to see if it matches what was requested if not then it loops around and should get the next block if not and it gets to EOF then it should stop looping and print that it couldent locate the file then exit and return to the menu. What happens is that i reads the first block and then seams to find a EOF cause it never tries to read the second block.

I dont have interent access at home ATM so i cant show you the current example of what im doing its very unforunate as this hinders your help. I am thinking at this point tho to abandon what i was trying to do as it creates more problems then it solves i guess. what my idea was to have a list of structures located on a file on the computer and have a special flag set on each block of data that determines if the block is free or not and if it is then to overwrite it. basicly it would write and read from the file when trying to locate or add a entry. What im leaning tword is to keep the list of information in memory as a linked list and then do a write operation for all the data at once when the user wants to save the file. and keeping the file a text file so i can check for problems if i need to. Keeping the data in binary format i cant check to see if the information is being written to as i intend.

anyway if anyone is intrested i can get the entire program as is and send it to you via email as a tar file.
( the old one of course as i havent started writeing the new version )

Last edited by exvor; 09-25-2006 at 04:21 PM.
 
Old 09-26-2006, 08:56 AM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
I figured it out, It was a problem in the way I was checking for the end of a file. I was using this statement in a while loop when reading the file.

while (feof(datfile) != 0)

this is not correct cause if its not = 0 then it means that the statement is true and the while loop will end. I merly changed it to while (feof(datfile) == 0) so that it will continue untill it gets to the end. I do however need to rewrite the code so that im not fseeking past the end of the file.

Thanks everyone for your help

Last edited by exvor; 09-26-2006 at 08:57 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fwrite in another fwrite? ati Programming 2 05-11-2006 02:25 AM
what could cause this fwrite() to seg fault? hedpe Programming 11 02-12-2006 10:02 PM
fwrite() doe not move pointer, why? realos Programming 4 12-08-2005 12:48 PM
C Unix Programming: getting info from dirent structures MDBlueIce Programming 2 06-24-2005 06:02 PM
fread magic: Input/Output error LoungeLizard Linux - Hardware 1 02-24-2004 06:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration