CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2013
    Posts
    1

    loop error in c++

    i ,

    I am trying to create a loop to call in the entries from the text file named Set.
    The entries of Set.txtare :
    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[i]=='_' && 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. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: loop error in c++

    When posting code, please use code tags. Go advanced, select code and click '#'.

    I thought at first you were using c until I say the cout statement! A way to generate the file names is

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    const string basefilename = "d:/data/";
    const string nameprefix = "tst_data";
    const string namesuffix = ".txt";
    
    int main()
    {
    string filename;
    
    char	num[10] = {0};
    
    	for (int i = 1; i <= 64; i++) {
    		filename = basefilename + nameprefix + itoa(i, num, 10) + namesuffix;
    		cout << filename << endl;
    	}
    
    	return 0;
    
    }
    Note that if you are using c++11, then itoa(..) can be replaced with to_string(i) and the num definition would not be required.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured