for loop doubt

Discussion in 'C' started by wasiy102, Jul 14, 2013.

  1. wasiy102

    wasiy102 New Member

    Joined:
    Jul 14, 2013
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hiii ,

    I am trying to create a loop to call in the entries from the text file named Set.
    The entries of Set.txt are :
    1 2 3
    2 4 5
    and so on .. (64 such combinations)[/CODE]

    it basically means the first combination for testing is 1 2 3 and next has to be 2 4 5 and so i have 64 such entries defined in set

    my test files are located in D://data// and are named tst_data1 to tst_data64.


    i created a loop for test set but its incorrect

    Code:
    // loop for test samples
    char basefilename[] = "D://data//";
    char , testFilen[160], numiChar[10];

    for (int i=1; i<=64; i++) {
    strcpy(basefilenme, "D://data//");
    strcat(testfilename, Set[]);


    if (testfilenme=='_' && testfilenme[i+1]=='\0'){
    strcat(testfilename, numiChar);
    strcat(testfilename, "_");
    break;
    }

    strcat(testfilename, ".txt");
    cout<<testfilename<<endl;

    please correct me how can i call the Set .txt and how to define it.

    thanks
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    I'm not sure what you're asking about. I *see* that want your file names to look like d://data//tst_data1...tst_data64 is that right? If it is, then using sprintf would be a lot eaiser.

    Code:
    for(i=1; i <= 64; ++i)
    {
        memset(buffer, 0, sizeof(buffer)); // clear out any trash characters
        sprintf(buffer, "d://data//tst_data%d", i); // tst_data1 - tst_data64
    }
    
    If you're building these as 64 separate file names to open, then in this loop try something like

    Code:
    FILE *fp;
    
    loop(...)
    {
        // build string as above
    
        if((fp = fopen(buffer, "r")) != NULL)
        {
             char line[100];
             while(fgets(line, sizeof(line), fp) != NULL)
             {
                  // do something with line
              }
    
              fclose(fp);
        }
    }
    if each digit on your lines is separated by a space, then you can use sscanf to pick them off line.

    Code:
    int digit1, digit2, digit3;
    
    while(fgets(line, sizeof line, fp) != NULL)
    {
        if((sscanf(line, "%d %d %d", &digit1, &digit2, &digit3)) != 3)
            // error parsing line
        else
            // do something with digit1, digit2, and digit3
    }
    If it just one integer, then change the sscanf call to

    Code:
    if((sscanf(line, "%d", &number)) != 1)
        // error parsing line
    else
        // do something with number
    
    Hope that helps
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice