problem with reading multiple files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anjanaanupindi
    New Member
    • Sep 2007
    • 6

    problem with reading multiple files

    I have the following problem: My program is supposed to read data from files to an array. The files are numbered: capture[1].bmp,capture[2].bmp and so on. And I really don't want to do the same step for so many times. Is there a way to write a loop along the lines of

    for (int i=1; i<25, i++);
    {
    fopen ("C:\capture[i].bmp","r")
    fread(ch,sizeof (ch),1,fp);
    .....
    }

    In other words, is there a way to insert the "i" variable into the filename?

    Thank you for your insights.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by anjanaanupindi
    I have the following problem: My program is supposed to read data from files to an array. The files are numbered: capture[1].bmp,capture[2].bmp and so on. And I really don't want to do the same step for so many times. Is there a way to write a loop along the lines of

    for (int i=1; i<25, i++);
    {
    fopen ("C:\capture[i].bmp","r")
    fread(ch,sizeof (ch),1,fp);
    .....
    }

    In other words, is there a way to insert the "i" variable into the filename?

    Thank you for your insights.
    Yes, there is:
    [code=c]
    fopen ("C:\capture[" + i + "].bmp", "r");
    [/code]

    Comment

    • anjanaanupindi
      New Member
      • Sep 2007
      • 6

      #3
      Originally posted by ilikepython
      Yes, there is:
      [code=c]
      fopen ("C:\capture[" + i + "].bmp", "r");
      [/code]


      thank you............ .......but i f i do that it's showing an error like



      expression must have integral type
      fopen ("C:\capture[" + i + "].bmp", "r");


      actually wht is that + i + operator....... .......to use that we have to include any headre file

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by anjanaanupindi
        thank you............ .......but i f i do that it's showing an error like



        expression must have integral type
        fopen ("C:\capture[" + i + "].bmp", "r");


        actually wht is that + i + operator....... .......to use that we have to include any headre file
        I believe your error is being caused by an int being inserting into a string, I would recommend using a stringstream to convert the int to a char/string that can then be used in that fashion.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Your file isn't opening. Since you don't check that, you wouldn't know.

          The problem is the path. \c is a carriage return and not two characters \ and c.

          Try

          [code=c]
          fopen ("C:\\captur e[i].bmp","r");
          [/code]

          Besides you didn't have a ; at the end of your fopen si the code may not have compiled at all.

          The next thing is to get the [i] outdside the literal (see Post #2).

          Comment

          • anjanaanupindi
            New Member
            • Sep 2007
            • 6

            #6
            Originally posted by weaknessforcats
            Your file isn't opening. Since you don't check that, you wouldn't know.

            The problem is the path. \c is a carriage return and not two characters \ and c.

            Try

            [code=c]
            fopen ("C:\\captur e[i].bmp","r");
            [/code]

            Besides you didn't have a ; at the end of your fopen si the code may not have compiled at all.

            The next thing is to get the [i] outdside the literal (see Post #2).

            Thank You for ur suggetion...... .....

            but if i do the same also same thing means it's opening the file but it's not reading any thing from file


            fopen ("C:\\captur e[k].bmp", "r");


            fread(&ch,sizeo f(ch),1,fp);

            fclose(fp);

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by anjanaanupindi
              fopen ("C:\\captur e[k].bmp", "r");
              Your file is not opening because the file name is not C:\capture[k].bmp.

              Its probably something like C:\capture25.bm p

              So you need to build the file name correctly.

              Apparently you are using C:
              [code=c]
              char* filename[80];
              filename[0] = '\0';
              strcat(filename , "C:\\captur e");
              strcat(filename , k + '0'); //convert the int to a char
              strcat(filename , ".bmp");
              FILE* f = fopen(filename, "r");
              if (f == NULL)
              {
              printf("file did not open\n");
              exit(1);
              }
              [/code]

              Comment

              Working...