Help with FIle handling!!!

This is a discussion on Help with FIle handling!!! within the C Programming forums, part of the General Programming Boards category; Friends, this is my code ..m jus trying to store some data as objects of structures in a file. and ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    Help with FIle handling!!!

    Friends, this is my code ..m jus trying to store some data as objects of structures in a file. and later retrieve them to do some operations... can someone pls help me wid it?? Thnx!!
    The code is kinda huge and messd up...so any help is highly appreciated..


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    struct source
    {
        char lbl[10];
        char opc[10];
        char op[10];
    };
    struct res
    {
        char lbl[10];
        char opc[10];
        char op[10];
        int add;
    };
    struct symtab
    {
        char lbl[20];
        int add;
    };
    struct symtab t[20],t1[20];
    struct res r[20],r1[20];
    struct source s[20],s1[20];
    int n;
    
    void create()
    {
        int i=0,f=0;
        FILE *fp;
        fp=fopen("Input2","w+");
        //f=1;
        for(i=0;i<n;i++)
        {
           //    if(f==1)
    
         //        fp=fopen("Input.dat","a");
    
        printf("Please enter label #%d: ",i+1);
    //    fflush(stdin);
        scanf("%s",&s[i].lbl);
        printf("Please enter opcode #%d: ",i+1);
        //fflush(stdin);
        scanf("%s",&s[i].opc);
        printf("Please enter operand #%d: ",i+1);
        //fflush(stdin);
        scanf("%s",&s[i].op);
        fwrite(&s[i],sizeof(s),1,fp);
    
        }
        fclose(fp);
    }
    
    void read()
    {
        struct source s1[20];
        int i=0;
        FILE *fp1;
        fp1=fopen("Input2","r");
    
           //    for(i=0;i<=n;i++)
           //    {
              //    fflush(stdin);
               while(fread(&s1[i],sizeof(s1),1,fp1)!=0)
               {
            printf("\n\nLabel : %s\tOpcode : %s\tOperand : %s ",s1[i].lbl,s1[i].opc,s1[i].op);
              i++;
        }
        //}
    
    
           fclose(fp1);
    
    }
    int x=0;
    void process()
    {
        FILE *fp,*fp1,*fp2,*fp3,*fp4, *fp5;
    
        int i,j,k;
        struct source s2[20];
        char lbl[20],opc[20],op[20],opcode[20],label1[20],opc1[20],op1[20],add1;
        fp=fopen("Input2","r");
        fp3=fopen("symtab2","w+");
        printf("\nNow processing file : \n\n");
    
        for(i=0;i<n;i++)
        {
            fread(&s2[i],sizeof(s2),1,fp);
            printf("\nLabel #%d: ",i+1);
            printf("%s",s2[i].lbl);
            printf("\nOpcode #%d: ",i+1);
            printf("%s",s2[i].opc);
            printf("\nOperand #%d: ",i+1);
            printf("%s",s2[i].op);
            strcpy(lbl,s2[i].lbl);
            strcpy(opc,s2[i].opc);
            strcpy(op,s2[i].op);
    
            if(strcmp(opc,"start")==0 || strcmp(opc,"START")==0)
                {x=x+atoi(op);}
    
    
            if(strcmp(opcode,"RESW")==0)
            {
                x=x+(atoi(op[i])*3);
    
            }
            else if(strcmp(opcode,"RESB")==0)
            {
                x=x+(atoi(op[i])*1);
            }
            else if(strcmp(opcode,"BYTE")==0)
            {
                x=x+1;
            }
            else
            {
                x=x+3;
            }
    
            fp2=fopen("output2","w+");
            strcpy(r[i].lbl,lbl);
            strcpy(r[i].opc,opc);
            strcpy(r[i].op,op);
            r[i].add=x;
            fwrite(&r[i],sizeof(r),1,fp2);
            if(lbl!="null" || lbl!="NULL")
            {
    
                   strcpy(t[i].lbl,lbl);
                   t[i].add=x;
                   fwrite(&t[i],sizeof(t),1,fp3);
            }
    
        }
        fclose(fp2);
        fclose(fp3);
        fclose(fp);
    
        printf("Now beginning to display Output Table: \n");
        fp4=fopen("output2","r");
        for(k=0;k<n;k++)
        {
            fread(&r1[i],sizeof(r1),1,fp4);
            strcpy(label1,r1[i].lbl);
            strcpy(opc1,r1[i].opc);
            strcpy(op1,r1[i].op);
        }
    
        printf("\n\nAdd: %d\tLabel: %s\tOpcode: %s\tOperand: %s",label1,opc1,op1);
        getch();
        fclose(fp4);
        printf("Now beginning to display Symbol Table: \n");
        fp5=fopen("symtab2","r");
        for(k=0;k<n;k++)
        {
            fread(&t1[i],sizeof(t1),1,fp5);
            strcpy(label1,t1[i].lbl);
            add1=t1[i].add;
            printf("\n\nLabel: %s\tAddress: %d",label1,add1);
        }
        fclose(fp5);
        getch();
    }
    
    
    
    
    void main()
    {
        clrscr();
        printf("Please enter no. of lines: ");
        scanf("%d",&n);
        create();
        //read();
        getch();
        process();
        getch();
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Give us the details here - what is "messed up"?

    Remember, we don't have your files, so running your program to see what's wrong with it, is out of the question. Also, you SHOULD know, already, and be able to tell us.

  3. #3
    rey
    rey is offline
    Registered User
    Join Date
    Feb 2012
    Posts
    9

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,238
    Quote Originally Posted by rey View Post
    Senseless post, followed by a plug.

    FYI: "conio.h" is a non-standard header, and is only supported on certain systems. Furthermore, it only seems to be used to prevent the console from closing. There are better, portable methods to accomplish this. This "tutorial" is placing needless restrictions on whom will be able to follow along.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    333
    Yes, I spotted some local problems with the code, like the comparison to a string using != instead of strcmp().

    But the real issue is organisation.
    You should have one structure for your records.
    Then you have one file format, and a matching pair of functions to read and write the file.

    A lot of files consist of a record, repeated a variable number of times. You need to decide how you store this variable number. An easy way is to write it at the
    start of the file. However that makes the file harder to read. The other way is to count the records, and detect when they run out. This makes the reader harder to program.

    So you should have a struct record;

    Then

    Code:
    /*
      write records, return -1 on failure
    */
    int writerecords(struct record *data, int N, char *filename)
    
    /*
      read the records. Return in allocated memory. N is the return for number of records read. Return 0 on failure
    */
    struct record *readrecords(char *filename, int *N)
    You then test the functions against each other (you can also look at the output file). When you can write and recover the same data, you know that it's probably working OK.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File handling
    By sick in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 07:08 PM
  2. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 01:01 PM
  3. FILE handling
    By Munisamy in forum C Programming
    Replies: 0
    Last Post: 02-27-2005, 11:04 PM
  4. C file handling
    By valar_king in forum C Programming
    Replies: 5
    Last Post: 01-22-2002, 02:32 PM

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