I/O help...I think,,,

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cap213
    New Member
    • Aug 2007
    • 3

    I/O help...I think,,,

    This part of C always gets me...it shouldn't...but it does, so here I go:
    I've looked everywhere but I can't find the answer anwhere

    void Arrays (int numArray[], int quantity[])

    {
    FILE *fp;
    fp = fopen("stock.da t", "rb+");
    fclose(fp);
    }

    if I wanted to put the data from the file into the two arrays how would I go about doing this? I can only find the explanations of to read in one..how would I go about reading the record layout in?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      This code:
      Originally posted by cap213
      void Arrays (int numArray[], int quantity[])

      {
      FILE *fp;
      fp = fopen("stock.da t", "rb+");
      fclose(fp);
      }
      will be hard for you to use. That is, when you pass an array to a function, all that is passed is the address of element 0. So here you have two int pointers for your two arrays but you do not have the number of elements in these arrays.

      You need to tell fread how much to read.

      Comment

      • cap213
        New Member
        • Aug 2007
        • 3

        #4
        I think I'm figuring it out...if I wanted to scan the first half of the line, which is set up 348247:title. How would I accomplish this...I've tried this...but it does not seem to be working...(the title which it is comparing to is passed from another fucntion..)

        int search(char title[81], int *stocknum[])
        {
        int j;
        char tite[81];
        FILE *va;
        va = fopen("/Users/cap412/Desktop/videos.dat", "r");
        for (j = 0; j < 50; j++){
        fscanf(va, "%s", tite[j]);
        if(strncmp(tite , title, 81) == 0)
        fscanf(va, "%[0-9]", &stocknum[j]);
        fclose(va);
        }
        }

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          The first thing is to stop using fscanf(). This thing skips whitespace.

          Use fread() and read in the correct number of bytes or use fgets() which will read in an entire line of text, which you can then parse.

          Comment

          • cap213
            New Member
            • Aug 2007
            • 3

            #6
            Originally posted by weaknessforcats
            The first thing is to stop using fscanf(). This thing skips whitespace.

            Use fread() and read in the correct number of bytes or use fgets() which will read in an entire line of text, which you can then parse.
            okay, so it can read in, only, the line where the text is located?

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              I assume you know the file format. Yes?

              Let's assume the file format is two arrays of 10 ints each.

              [code=c]
              int readbuffer[20]

              fread(readbuffe r, 20 * sizeof(int), 1, myfile);
              [/code]

              Then just distribute the readbuffer array to your two arrays. You know that the first array is readbuffer[0] through readbuffer[9], etc.

              I'm just guessing here as I don't know your file format.

              Comment

              Working...