Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Reading a comma delimited file to array

Reading a comma delimited file to array

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresregexquestionannouncement
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeffrey Webster
    wrote on last edited by
    #1

    Hi, I'm hitting a snag as I try to read back an array which has been written to a text file. Here's the relevant part of the code:

    if((fp=fopen("myfile","r"))==NULL)
    {
    printf("Unable to open file.\n");
    exit(1);
    }
    for(j=0;j<6;j++)
    {
    for(l=0;l<7;l++)
    {
    k=fgetc(fp);
    if(k==",") break;
    str2[j][l]=k;

      }    
    

    }
    fclose(fp);
    for(j=0;j<6;j++)
    {
    for (l=0;l<7;l++)
    {
    printf("%c", str2[j][l]);
    }
    }

    This is generating an error: [Warning] comparison between pointer and integer. Though it runs, it is revealing that the break code which checks for a comma is not functioning as the compiler warns. Why is it claiming this is a pointer? Why is it saying anything about an integer when I've deliberately cast k as a character? Thanks for any input. ------ Update: The problem was solved by: (a) changing the double quotes to single quotes (apostrophes) and (b) switching

    if(k==",") break;

    below the assignment. In order to match the file derived string with one defined in code I also had to modify it:

    str2[j][strlen(str2[j]-1]='\0';

    as there is something foreign added in the file transfer process. Thanks, and if there is a mark as solved button I couldn't find it:)

    S L 2 Replies Last reply
    0
    • J Jeffrey Webster

      Hi, I'm hitting a snag as I try to read back an array which has been written to a text file. Here's the relevant part of the code:

      if((fp=fopen("myfile","r"))==NULL)
      {
      printf("Unable to open file.\n");
      exit(1);
      }
      for(j=0;j<6;j++)
      {
      for(l=0;l<7;l++)
      {
      k=fgetc(fp);
      if(k==",") break;
      str2[j][l]=k;

        }    
      

      }
      fclose(fp);
      for(j=0;j<6;j++)
      {
      for (l=0;l<7;l++)
      {
      printf("%c", str2[j][l]);
      }
      }

      This is generating an error: [Warning] comparison between pointer and integer. Though it runs, it is revealing that the break code which checks for a comma is not functioning as the compiler warns. Why is it claiming this is a pointer? Why is it saying anything about an integer when I've deliberately cast k as a character? Thanks for any input. ------ Update: The problem was solved by: (a) changing the double quotes to single quotes (apostrophes) and (b) switching

      if(k==",") break;

      below the assignment. In order to match the file derived string with one defined in code I also had to modify it:

      str2[j][strlen(str2[j]-1]='\0';

      as there is something foreign added in the file transfer process. Thanks, and if there is a mark as solved button I couldn't find it:)

      S Offline
      S Offline
      Sivaraman Dhamodharan
      wrote on last edited by
      #2

      Try this: k=fgetc(fp); if(k==',') break; Hope that will work.

      Programming Article

      J 1 Reply Last reply
      0
      • S Sivaraman Dhamodharan

        Try this: k=fgetc(fp); if(k==',') break; Hope that will work.

        Programming Article

        J Offline
        J Offline
        Jeffrey Webster
        wrote on last edited by
        #3

        Hi, Thanks for the reply. As far as I can see that's what I have except without the assignment to the array:

        str2[j][l]=k

        but that is basically what I'm trying to accomplish. The printing at the end is just to confirm that the file was successfully written to the array.

        G 1 Reply Last reply
        0
        • J Jeffrey Webster

          Hi, Thanks for the reply. As far as I can see that's what I have except without the assignment to the array:

          str2[j][l]=k

          but that is basically what I'm trying to accomplish. The printing at the end is just to confirm that the file was successfully written to the array.

          G Offline
          G Offline
          Graham Breach
          wrote on last edited by
          #4

          Replace the double-quotes in your comparison with single-quotes. In C and C++, double-quotes mean a pointer to a string of characters, but single-quotes mean a character literal.

          J 1 Reply Last reply
          0
          • J Jeffrey Webster

            Hi, I'm hitting a snag as I try to read back an array which has been written to a text file. Here's the relevant part of the code:

            if((fp=fopen("myfile","r"))==NULL)
            {
            printf("Unable to open file.\n");
            exit(1);
            }
            for(j=0;j<6;j++)
            {
            for(l=0;l<7;l++)
            {
            k=fgetc(fp);
            if(k==",") break;
            str2[j][l]=k;

              }    
            

            }
            fclose(fp);
            for(j=0;j<6;j++)
            {
            for (l=0;l<7;l++)
            {
            printf("%c", str2[j][l]);
            }
            }

            This is generating an error: [Warning] comparison between pointer and integer. Though it runs, it is revealing that the break code which checks for a comma is not functioning as the compiler warns. Why is it claiming this is a pointer? Why is it saying anything about an integer when I've deliberately cast k as a character? Thanks for any input. ------ Update: The problem was solved by: (a) changing the double quotes to single quotes (apostrophes) and (b) switching

            if(k==",") break;

            below the assignment. In order to match the file derived string with one defined in code I also had to modify it:

            str2[j][strlen(str2[j]-1]='\0';

            as there is something foreign added in the file transfer process. Thanks, and if there is a mark as solved button I couldn't find it:)

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Why are you making life so complicated for yourself? Use a tokeniser such as strtok()[^].

            J 1 Reply Last reply
            0
            • G Graham Breach

              Replace the double-quotes in your comparison with single-quotes. In C and C++, double-quotes mean a pointer to a string of characters, but single-quotes mean a character literal.

              J Offline
              J Offline
              Jeffrey Webster
              wrote on last edited by
              #6

              Hi, thanks for your response. That helped. I also changed the control in the inside printf loop to

              for(l=0;*(str2[j]+l);l++)

              Now it's compiling with no warnings and is reading back pretty good text, though there is some stray characters after some of the words in the test text.

              1 Reply Last reply
              0
              • L Lost User

                Why are you making life so complicated for yourself? Use a tokeniser such as strtok()[^].

                J Offline
                J Offline
                Jeffrey Webster
                wrote on last edited by
                #7

                Thanks Richard, I'm going to try to track down the errors in this code first and then experiment with this tokenizer. At this point I'm trying to get a really granular understanding of C which even means reinventing the wheel at times, just for my own edification:)

                D L 2 Replies Last reply
                0
                • J Jeffrey Webster

                  Thanks Richard, I'm going to try to track down the errors in this code first and then experiment with this tokenizer. At this point I'm trying to get a really granular understanding of C which even means reinventing the wheel at times, just for my own edification:)

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Just glancing at your code I would suggest using proper names for variables. Also, read up on the difference between a character and a string.

                  J 1 Reply Last reply
                  0
                  • J Jeffrey Webster

                    Thanks Richard, I'm going to try to track down the errors in this code first and then experiment with this tokenizer. At this point I'm trying to get a really granular understanding of C which even means reinventing the wheel at times, just for my own edification:)

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Jeffrey Webster wrote:

                    At this point I'm trying to get a really granular understanding of C which even means reinventing the wheel at times, just for my own edification:)

                    A good strategy. :thumbsup:

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                    1 Reply Last reply
                    0
                    • L Lost User

                      Just glancing at your code I would suggest using proper names for variables. Also, read up on the difference between a character and a string.

                      J Offline
                      J Offline
                      Jeffrey Webster
                      wrote on last edited by
                      #10

                      Thanks. This is just a test of concept program so I'm not doing 'real' variable names. As a former VB person I'm still in the CCAC (code cuss and cry) phase of learning C:)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups