reading multiple strings from single row....

This is a discussion on reading multiple strings from single row.... within the C Programming forums, part of the General Programming Boards category; Hello Every1, I am stuck at the very starting step of my code. I have to read a file which ...

  1. #1
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96

    reading multiple strings from single row....

    Hello Every1,

    I am stuck at the very starting step of my code.
    I have to read a file which looks like this

    #city1 #city2 #distance
    delhi mumbai 2005
    calicut goa 3294


    I wan to read these in different arrays.
    I wrote something like this

    Code:
    char *pC1 = NULL,*pC2= NULL;
    float *pC3;
     FILE *fp;
     fp = fopen("String_DNA_CombinedScore.txt","r"); 
      int i, edges=0;
      char name[10];
    
      while(fgets(names,10,fp2) != NULL)
     {
    count++;
        }
      printf("The total number of cities: %d\n", count); 
    pC1 = (char*) calloc(edges, sizeof(char));
    pC2 = (char*) calloc(edges, sizeof(char));
    pC3 = (int*) calloc(edges, sizeof(int));
    
     rewind(fp2);
      for (i=0;i<count;i++)
        {
          fscanf(fp,"%s %s %f", &pC1[i], &pC2[i], &pC3[i]);
          printf("%c%c %f \n", pC1[i], pC2[i], pC3[i]);  
          
          }
        return edges;
    }
    thanks

  2. #2
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    m getting bus error every time...
    why?
    I have allocated memory here!

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    And how much have you allocated?

  4. #4
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    Code:
    pC1 = (char*) calloc(count, sizeof(char));
    pC1 is allocated that stores the names of column1 , count is the total number

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    That's not the code you posted says; to repeat:
    Code:
    pC1 = (char*) calloc(edges, sizeof(char));
    pC2 = (char*) calloc(edges, sizeof(char));
    Note there is no need to cast the return value of malloc/calloc in C.

    Also, why are you allocating int *s for an array of floats?

    Code:
    float *pC3;
    ...
    pC3 = (int*) calloc(edges, sizeof(int));

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Code:
    int i, edges=0;
    ...
    pC1 = (char*) calloc(edges, sizeof(char));
    pC2 = (char*) calloc(edges, sizeof(char));
    pC3 = (int*) calloc(edges, sizeof(int));
    calloc basically returns count*size bytes where the first argument is the count (edge) and the second argument is the size of a single element. In your case, since the count is 0 what do you think happens? I bet you've got bad pointers.

    Further...
    Code:
    FILE *fp;
    fp = fopen("String_DNA_CombinedScore.txt","r"); 
    ...
    char name[10];
    
    while(fgets(names,10,fp2) != NULL)
    {
        count++;
    }
    
    rewind(fp2);
    for (i=0;i<count;i++)
    {
        fscanf(fp,"%s %s %f", &pC1[i], &pC2[i], &pC3[i]);
    Where is fp2 declared? Do you really mean fp? And name vs. names? Is this a fragment of code that you've actually managed to get compiled?

    Also, I don't think this is really what you want to have happen. You're trying to load different strings into a single dimension character array. You probably want a two-dimensional array (an array of strings).
    Last edited by hk_mp5kpdw; 08-05-2010 at 11:20 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    Code:
    int EDGES()
    {
    FILE *fp2;
    fp2 = fopen("String_DNA_CombinedScore.txt","r"); 
    int i, edges=0;
    char c;
    while(fgets(names,10,fp2) != NULL)
    {
      edges++;
    }
    printf("\tThe total number of edges: %d\n", edges);   // number of edges in the network
    pC1 = (char*) calloc(edges, sizeof(char));
    pC2 = (char*) calloc(edges, sizeof(char));
    pC3 = (float*) calloc(edges, sizeof(float));
    rewind(fp2);
    for (i=0;i<edges;i++)
        {
          fscanf(fp2,"%c %c %f", &pC1[i], &pC2[i], &pC3[i]);
          printf("%c%c %f \n", pC1[i], pC2[i], pC3[i]);   // REading the  edge file 
        }
        return edges;
    }
    But i get BUS Error again.....

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Check fopen(),calloc(), fscanf(), rewind(), any functions that you use are successful or not.
    Also why don't you use a debugger?

    Also what do you want to do with the data from the file.(return to caller? or what?)
    Currently according to your function implementation, there's memory leak.
    Last edited by Bayint Naung; 08-06-2010 at 02:27 AM.

  9. #9
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    sure...
    but do u see any error in the code...or may be the logic....

  10. #10
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96

    Unhappy

    Seriously I have no idea how to read some file that has 2 strings and a decimal value....

    abc xyz 1.67
    pqr efg 2.56
    ABC XYZ 2.54

    i tried to read them in array as i mentioned before in the post but doesnt work...
    any suggestions here...

  11. #11
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    1) A successful opening followed by a successful closing (for file).

    2) while(fgets(names,10,fp2) != NULL) I can see a delimiter of ten chars. Does it represent also a delimiter for your file format or just for test ? If it's not a test then why don't use an array of 10-char strings ?

    3) fscanf(fp2,"%c %c %f", &pC1[i], &pC2[i], &pC3[i]); Reading chars not strings.

    4) You don't free allocated memory.

    5) What's the point of reading data from file as you return the number of edges ?
    Last edited by ch4; 08-06-2010 at 04:39 AM.

  12. #12
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96

    Unhappy

    thanks.....
    I dint get the second point...

    else i corrected but i dont know why m getting bus error, I ahve allocated enuff space for the strings....

    or shd i first separate the columns by "space" delimiter and try to read in arrays...
    can suggestions?

  13. #13
    Registered User
    Join Date
    Aug 2010
    Posts
    2
    once read the line with fgets, why don't you use strtok?

    strtok - C++ Reference

  14. #14
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    can u help me with a small code....
    i read it...but not able to implement...
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Excel Row Cells A through J
    By CheyenneWay in forum C++ Programming
    Replies: 37
    Last Post: 03-17-2011, 08:00 AM
  2. reading pictures and array
    By sunoflight77 in forum C++ Programming
    Replies: 0
    Last Post: 05-09-2005, 03:16 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 06:24 PM
  5. Is there a bug in this part of my algorithm for connect 4?
    By Nutshell in forum Game Programming
    Replies: 8
    Last Post: 04-28-2002, 01:58 AM

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