sequential file help plz

This is a discussion on sequential file help plz within the C Programming forums, part of the General Programming Boards category; Hey guys, im new to C programming and after a few classes, I'm still stuck on trying to figure out ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    Unhappy sequential file help plz

    Hey guys, im new to C programming and after a few classes, I'm still stuck on trying to figure out this program concerning sequential files. The thing is, I have a majority of the program written, but I believe there are major flaws in the code (at least the compilier im using tells me this). I dont want anybody to do it for me, but I dont understand how pointers work in structs, methods, etc. I hope i didn't confuse you already with what I'm trying to say, but please advise.

    Code:
     
    #define MAXKEY 999
    #define NAMELEN 7
    #define SENTINEL -1
    #define MAXTESTPTS 120
    #define MAXHWCOUNT 10
    #define MAXHWPOINTS 50
    
    #define TRUE 1
    #define FALSE 0		 
    
    struct StudentRecord {	
     int    	id;      
     char   name [NAMELEN]; 
      int    	test1;                // score of first test
      int    	test2;               // score of second test
      int    	test3;                 // score of third test
      int 	hwCount;          // number of hws completed
      int 	hwPoints;         // points accumulated on hw
    } dummyRecord = {SENTINEL, "", 0, 0, 0, 0, 0}; 
                         // dummy file used to present format? not sure
    
    struct TransactionRecord {
      int	kind;			
                    // holds values 0 to 5 */
    	// 0 = Insert, 1-3 = Modify Test Score k	
    	// 4 = Add HW assignment & points, 
                    //  5 = Add HW points w/o assignment)
    
      int	id;  
      char	name [NAMELEN];  
      int 	value;           
    } dummyTrans = {SENTINEL, SENTINEL, "", 0};
                     // dummy file
    
    // temporary data types used for intermediate reading and writing
    int tkind;
    int tid;
    char tname [NAMELEN];
    int tvalue;
    
    
    
    file  *inputFile;
    file  *recordFile;
    file  *transFile;
    file *newRecordFile;
    file  *errorFile;
    //  declarations used for holding files 
    
    int numOldRecords, numNewRecords;
    	
    int	numTrans, numErrors;
    		
    char *  internalError  = "Internal error. Sorry. \n";
    
    char *	redundantInsert = "Attempt to insert duplicate record with key %d";
    
    char *	invalidInsert = "Can't insert record with key %d; record already exists\n";
    
    char *	invalidModify = "Invalid modify operation with key %d; no record to modify\n";
    
    char *	invalidDelete = "Invalid delete operation with key %d; no record to delete\n";
    
    char *	numberError = "Invalid update with key %d; numerical constraint violated\n";
    
    int openFiles ( ){
    
        if ((inputFile = fopen ("inputfile.txt", "r")) == NULL) 
      {
    
        fprintf (stderr, "Cannot open input file\n");
        return -1; 
      }
    
      if ((recordFile = fopen ("recordfile.txt", "w")) == NULL) 
      {
        fprintf (stderr, "Cannot open report file\n");
        return -1; 
      }
      if ((transFile = fopen ("transfile.txt", "r")) == NULL) {
        fprintf (stderr, "Cannot open file to hold data\n");
        return -1; }
      if ((newRecordFile = fopen ("newrecordfile.txt, w+" == NULL){
        fprintf (stderr, "Cannot open new record file\n");
        return -1;}
      if ((errorFile = fopen ("errorfile.txt, r+" == NULL){
        fprintf (stderr, "Cannot open error file\n");
        return -1;}
      else 	/* all files opened successfully */
        return 0;
    };
    
    void closeFiles ( ){
      fclose (inputFile);
      fclose (recordFile);
      fclose (newRecordFile);
      fclose (reportFile);
      fclose (transFile);
      fclose (errorFile);
    }
     
            
    	
    int getTransFromTransFile (transFile, tkind, tid, tname[NAMELEN]){                
      if(fseek(transFile, %d) != NULL){
           return -1;
    }else{                          
         tkind = fscanf(transFile,%d) // reads the type of the transaction and stores it
         fseek (transFile, %d);         // finds the next int, which is the id
         tid = fscanf(transFile,%d);  // stores that into the trans id 
         fseek (transFile, %s);
         tname[NAMELEN] = fscanf(transFile,%s);
         if(tkind != 0){     
    
    // here im assuming that an insert trans does not add hw or test scores
                  
          fseek(transFile, %d);
          int tvalue = fscanf(transFile,%d);
                  }
         fseek (transFile,"\n");  
         return 0;     
    }
         
    void logError (char * errMsg, int id)
    {
      fprintf (ReportFile, "%s %d\n", errMsg, id);
    }
    
    void createRecords(inputFile,recordFile, ){
         while(fseek(inputFile, %d) != NULL){
            // create  new student record if pass is not null
                StudRec = new StudentRecord();
                fseek(inputFile, "%d");
                StudRec.id = fwrite(recordFile, fscanf(inputFile,%d,&id), &id);
                // looks for next character in the file
                fseek(inputFile, "%s");
                StudRec.name = fwrite(recordFile, fscanf(inputFile,%s,&name), &name);
                fseek(inputFile, "%d");
                StudRec.test1 = fwrite(recordFile, fscanf(inputFile,%d,&test1), &test1);
                fseek(inputFile, "%d");
                StudRec.test2 = fwrite(recordFile, fscanf(inputFile,%d,&test2), &test2);
                fseek(inputFile, "%d");
                Studrec.test3 = fwrite(recordFile, fscanf(inputFile,%d,&test3), &test3);
                fseek(inputFile, "%d");
                Studrec.hwcount = fwrite(recordFile, fscanf(inputFile,%d,&hwcount), &hwcount);
                fseek(inputFile, "%d");
                Studrec.hwpoints = fwrite(recordFile, fscanf(inputFile,%d,&hwpoints), &hwpoints);
                fwrite(recordFile,"\n"); // after complete write of record, write new line
                numOldRecords ++;
                }
    }
     
    int copyRecords (recordFile, newRecordFile); // runs only when all transactions are complete
    	while(fseek(recordFile, %d) != NULL){
                StudRec = new StudentRecord();
                fseek(inputFile, "%d");
                fwrite(newRecordFile, fscanf(recordFile,%d,&id), StudRec.id);
                // looks for next character in the file
                fseek(inputFile, "%s");
                fwrite(newRecordFile, fscanf(recordFile,%s,&name), StudRec.name);
                fseek(inputFile, "%d");
                fwrite(newRecordFile, fscanf(recordFile,%d,&test1), StudRec.test1);
                fseek(inputFile, "%d");
                fwrite(newRecordFile, fscanf(recordFile,%d,&test2), StudRec.test2);
                fseek(inputFile, "%d");
                fwrite(newRecordFile, fscanf(recordFile,%d,&test3), StudRec.test3);
                fseek(inputFile, "%d");
                fwrite(newRecordFile, fscanf(recordFile,%d,&hwcount), StudRec.hwcount);
                fseek(inputFile, "%d");
                fwrite(newRecordFile, fscanf(recordFile,%d,&hwpoints), StudRec.hwpoints);
                return 0;
                }
     return -1;
    }
    
    void updateSequentialFile (transFile, tkind, tid, tname[NAMELEN], recordFile, newRecordFile){
    	while(getTransFromTransFile (transFile, tkind, tid, tname[NAMELEN]) != -1){
              fseek(recordFile,%d);
              int rid = fscanf(recordFile,%d);
              if (tid > rid)
              if(tid < rid && tkind == 0){ // its an insert
                    StudRec.id = new StdentRecord()
                    fwrite(newRecordFile, tid, StudRec.id);
                    fwrite(newRecordFile, " ");
                    fwrite(newRecordFile, tname[NAMELEN], StudRec.name);
                    fwrite(newRecordFile, " ");
                    
                    }
              // this part is ridiculous
              else if(tid == rid && tkind == 1){  //  modify test score 1
                    fwrite(newRecordFile, tid);   // already set 
                    fwrite(newRecordFile, " ");
                    fwrite(newRecordFile, fscanf(recordFile, StudRec.name);
                    fwrite(newRecordFile, " ");
                    fwrite(newRecordFile, tvalue);
                    fwrite(newRecordFile, " ");
                    fwrite(newRecordFile, fscanf(recordFile, StudRec.test2);
                    fwrite(newRecordFile, " ");
                    fwrite(newRecordFile, fscanf(recordFile, StudRec.test3);
                    fwrite(newRecordFile, " ");
                    fwrite(newRecordFile, fscanf(recordFile, StudRec.hwcount);
                    fwrite(newRecordFile, " ");
                    fwrite(newRecordFile, fscanf(recordfile, StudRec.hwpoints);
                    }
              else if(tid == rid && tkind == 2){
                   fwrite(newRecordFile, tid);   // already set 
                    fwrite(newRecordFile, " ");
                    fwrite(newRecordFile, fscanf(recordFile, StudRec.name);
                    fwrite(newRecordFile, " ");
                    fwrite(newRecordFile, tvalue);
                    fwrite(newRecordFile, " ");
                    fwrite(newRecordFile, fscanf(recordFile, StudRec.test2);
                    fwrite(newRecordFile, " ");
                    fwrite(newRecordFile, fscanf(recordFile, StudRec.test3);
                    fwrite(newRecordFile, " ");
                    fwrite(newRecordFile, fscanf(recordFile, StudRec.hwcount);
                    fwrite(newRecordFile, " ");
                    fwrite(newRecordFile, fscanf(recordfile, StudRec.hwpoints);
                    }
        numNewRecords ++;
        fwrite(newRecordFile,"\n"); // preparing for next pass
        fseek(recordFile, "\n");
    }
        while(copyRecords(recordFile, newRecordFile) != -1){
        numNewRecords ++;
        }
    }               
      
    
    
    void reportStatistics ( ){
         fprintf(stderr, ("Number of Old records: ", numOldRecords, "\n"));
         fprintf(stderr, ("Number of New records: ", numNewRecords, "\n"));
    
         
    void echoDataToScreen (){
         fprintf(stderr, "Process Completed. \n");
    }
    
    int main ( );
    	/* opens files, checks for error */
        if (openfiles () != 0){
          createRecords();
          updateSequentialFile();
          copyFiles();
          getStatistics();
          echoDataToScreen();
    }
        closeFiles();
           
        return;

    I got rid of most of the comments n such from this to simplify it. The assignment was to write a sequential file program that takes an original sequential file and a transaction file, and then writes a new modified sequential file (after modifications according to the transactions). This is really confusing, and any help here would be huge. Thanks in advance!

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    Is this the "X files" ?

    first of all, main should look like this
    Code:
    int main(void)
    {
    int  result;
    
    
    return(reult);
    }
    The return from main, should be within main. also all the functions call.

    fseek prototype :
    Code:
    int fseek(FILE *stream, long offset, int origion);
    to allocate new recored use malloc. Not new !

    Thats as far as I got.
    Good luck.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    1. There are NO includes in your program! As a minimum you will need to include stdio.h and probably string.h. add them at the top of your program, before anything else.
    Code:
    #include <stdio.h>
    #include <string.h>
    // other includes and the rest of your program here
    Code:
    file  *inputFile;
    file  *recordFile;
    file  *transFile;
    file *newRecordFile;
    file  *errorFile;
    C is case sensetive -- file must be spelled FILE, which is defined in stdio.h

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    if all files opened u return 0 so
    Code:
    if (openfiles () != 0){
    should be
    Code:
    if (openfiles () != -1){
    or
    if (openfiles () == 0){
    u got u see the man page of fscanf fwrite fseek. i seem that u are using fssek for some other purpose. it is used for ajusting the file pointer within a file

    ssharish2005

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    yeahh im very confused with the fscanf fwrite and fseek functions...

    thank you anyways all for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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