Txt Data File Reading In C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tweeny
    New Member
    • May 2007
    • 13

    Txt Data File Reading In C

    I have a txt file with data like this:

    A B C D E ...
    1 546 456 89 456
    2 6546 654 5646 6546
    3 444 565 789 789

    for example

    I would like to be able to read the file and be able to store one whole line in variables and then compare with the next line's numbers and then write the biggest line to a file (that part is easy)...I really need a procedure that can read each line one by one until end of file and store them into a variable...

    I know that is possible but i've always hated working with files...The rest is pretty easy, but i've never manage to work with files...

    I would really like some help please
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by tweeny
    I have a txt file with data like this:

    A B C D E ...
    1 546 456 89 456
    2 6546 654 5646 6546
    3 444 565 789 789

    for example

    I would like to be able to read the file and be able to store one whole line in variables and then compare with the next line's numbers and then write the biggest line to a file (that part is easy)...I really need a procedure that can read each line one by one until end of file and store them into a variable...

    I know that is possible but i've always hated working with files...The rest is pretty easy, but i've never manage to work with files...

    I would really like some help please
    Hi,
    here goes alghorytham:

    1) Loop while pointer to file don't reach EOF(end-of-file);

    2) use getline,fgets or any simmilar function to extract sngle line

    3) After loop compare lines

    Savage

    Comment

    • tweeny
      New Member
      • May 2007
      • 13

      #3
      Originally posted by Savage
      Hi,
      here goes alghorytham:

      1) Loop while pointer to file don't reach EOF(end-of-file);

      2) use getline,fgets or any simmilar function to extract sngle line

      3) After loop compare lines

      Savage


      ok...i understand what you wrote, but using getline how can i seperate the line which contains various variables into the individual variables???

      Comment

      • tweeny
        New Member
        • May 2007
        • 13

        #4
        probably for this part it would be better if you wrote a couple of lines in code with comments to see if i can finally get rid of this aquiles heel that i have

        Thank you for your help

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Check out the strtok function. Once you have a line in a single variable, you can use this to split it into the different chunks of data, and then use the atoi function or the atof function to get the number equivalent of each piece.

          Comment

          • tweeny
            New Member
            • May 2007
            • 13

            #6
            ok, i got to the part where i can get every string of each line...not bad...now, the parameters of strtok (it says pch = strtok (str," ,.-");, i dont really understand the part with the comma, dot and dash...are those the parameters? to split???because i believe doesnt really have a pattern of spaces to put in as a parameter...But i think i'm getting the ideia

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              Originally posted by tweeny
              ok, i got to the part where i can get every string of each line...not bad...now, the parameters of strtok (it says pch = strtok (str," ,.-");, i dont really understand the part with the comma, dot and dash...are those the parameters? to split???because i believe doesnt really have a pattern of spaces to put in as a parameter...But i think i'm getting the ideia
              Now, I haven't much used strtok myself, but from a glance, it looks like the " ,.-" (note the space before the comma) is a 'list' of characters that will be used to split. For instance, their sample string is split at spaces, commas, dashes, and periods - each character included in the strtok call. Further, the result tokens do not include these characters - each one is skipped.

              Comment

              • tweeny
                New Member
                • May 2007
                • 13

                #8
                ok...here are some sort of good news:

                i have created a file called costumers.dat with the following test data

                1000 123 100
                2000 12 200
                3000 43 300
                4000 53 400

                i have then tried to get the individual numbers like i asked if anyone knew how. I've managed to read line by line, and seperate using strtok function...but now i cannot save to a variable... I think it is because i declared pch as a pointer and cannot seem to get the data which is pointing (and get a segmentation fault). I think i am not doing anything wrong...

                i need help...i need this to work tomorrow...than k you guys Ganon11 and savage for your help.

                HERE is what i wrote...can someone fixe it to make it work please??i just need the variables to get the right values and i believe that i can do the rest



                #include <stdio.h>
                #include <stdlib.h>
                #include <math.h>
                #include <string.h>

                int main()
                {
                char name[30];
                double bu;
                int number;
                double bill;
                char A;
                char B;
                char C;
                char *pch;
                int i;
                FILE *cfPtr;
                if ((cfPtr=fopen(" costumers.txt", "r"))==NULL )
                printf("BEEP\n" );
                else
                {
                printf("jdksf hskdjfh skdjfh skdjf hsdkjf hsdkjfhs\n");
                fgets(name,30,c fPtr);
                while (!feof(cfPtr)){
                printf("%s",nam e);

                fgets(name,30,c fPtr);

                pch= strtok (name," ,.-");
                i=0;
                while (pch!=NULL)
                {
                i++;
                if (i==1) { A=*pch;
                printf("%s\n",A );}
                if (i==2) { B=*pch;
                printf("%s\n",B );}
                if (i==3) { C=*pch;
                printf("%s\n",C );}
                pch= strtok (name," ,.-");

                }
                }
                fclose(cfPtr);
                }
                return 0;
                }

                Comment

                • Savage
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1759

                  #9
                  Originally posted by tweeny
                  ok...here are some sort of good news:

                  i have created a file called costumers.dat with the following test data

                  1000 123 100
                  2000 12 200
                  3000 43 300
                  4000 53 400

                  i have then tried to get the individual numbers like i asked if anyone knew how. I've managed to read line by line, and seperate using strtok function...but now i cannot save to a variable... I think it is because i declared pch as a pointer and cannot seem to get the data which is pointing (and get a segmentation fault). I think i am not doing anything wrong...

                  i need help...i need this to work tomorrow...than k you guys Ganon11 and savage for your help.

                  HERE is what i wrote...can someone fixe it to make it work please??i just need the variables to get the right values and i believe that i can do the rest


                  }
                  If it is a pointer use strcpy to copy that string to another one which would be temporary string and then u can do with that string anything u wish.

                  Savage

                  Comment

                  • Savage
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1759

                    #10
                    #include <stdio.h>
                    #include <stdlib.h>
                    #include <math.h>
                    #include <string.h>

                    int main()
                    {
                    char name[30];
                    double bu;
                    int number;
                    double bill;
                    char A;
                    char B;
                    char C;

                    char *pch;
                    int i;
                    FILE *cfPtr;

                    i++;
                    if (i==1) { A=*pch;
                    printf("%s\n",A );}
                    if (i==2) { B=*pch;
                    printf("%s\n",B );}
                    if (i==3) { C=*pch;
                    printf("%s\n",C );}
                    pch= strtok (name," ,.-");

                    }
                    }
                    fclose(cfPtr);
                    }
                    return 0;
                    }

                    Can u tell me what is this supposed to do?(bolded code)

                    Savage

                    Comment

                    • tweeny
                      New Member
                      • May 2007
                      • 13

                      #11
                      Nops...still doesn't work...well i mean...it does work but the problem is that it copies the whole of the string again... i wrote strcpy(A,pch)

                      WHAT SHOULD I DO???

                      Comment

                      • Savage
                        Recognized Expert Top Contributor
                        • Feb 2007
                        • 1759

                        #12
                        Originally posted by tweeny
                        Nops...still doesn't work...well i mean...it does work but the problem is that it copies the whole of the string again... i wrote strcpy(A,pch)

                        WHAT SHOULD I DO???
                        As far as I can see A is not the string it's only the single char.

                        So I'm intrigued how compiler was not giving any errors or warnings becasue this function requires string not a simple char.

                        Savage

                        Comment

                        • tweeny
                          New Member
                          • May 2007
                          • 13

                          #13
                          I believe i understand what is wrong... strtok isnt working very well because i cannot figure out what are the conditions to split the string that is read...any ideias

                          please try to run the program and make it work...i'm in agony

                          Comment

                          • tweeny
                            New Member
                            • May 2007
                            • 13

                            #14
                            BOLDED...i want to copy the value that pch has to be able compare it with the next line...when i program that part

                            Comment

                            • tweeny
                              New Member
                              • May 2007
                              • 13

                              #15
                              Originally posted by Savage
                              As far as I can see A is not the string it's only the single char.

                              So I'm intrigued how compiler was not giving any errors or warnings becasue this function requires string not a simple char.

                              Savage
                              i've change A and the other variables to A[10]

                              Comment

                              Working...