problem loading array of strings

I am looking up file names. The code is not complete here,
When I compile it there are no errors, but when I run it,
1. It does find the files and prints the fName4 value.
2. It does not print the fName7 value.
What am I doing wrong?
How do I get the value into fName7[i1]?
How do I get it back out?

By the way, How do I corectly post the code?


void getNames (void)
{
int i1, i2, i3, i4, i5;
char ext1 [3];
char fName7 [100] [20]; // 100 of string[20]

done1 = false;
i1 = 0; i2 = 0; i3 = 0;
strcpy (ext1, "000");
cout << endl;

while (!done1)
{
strcpy (fName4, "enc_ref." );
strcat (fName4, ext1);
ifstream f4 (fName4);
if (f4.good())
{
strcpy (fName7[i1], fName4);
i1++;
cout << "\n Found file: " << fName7[i1] << " or1: " << fName4;
cout << " or2: " << fName7[i1][0] << fName7[i1][1] << fName7[i1][2];
}
f4.close();

ext1[2]++; // increment file extension
if (ext1[2] > '9')
{
ext1[2] = '0';
ext1[1]++;
if (ext1[1] > '9')
{
ext1[1] = '0';
ext1[0]++;
if (ext1[0] > '9') done1 = true;
}
} // end file extension
} // end while
goodNames = i1;
// deal with first blank ???
}

Use the library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <fstream>
#include <vector>
#include <iostream>
#include <sstream>
#include <iomanip>

std::vector<std::string> get_names()
{
    std::vector<std::string> good_names ;
    const std::string base_name = "enc_ref." ;

    for( int ext = 0 ; ext < 1000 ; ++ext )
    {
        std::ostringstream stm ;
        stm << std::setw(3) << std::setfill('0') << ext ;
        const std::string path = base_name + stm.str() ;

        std::cout << "check file: " << path << "  " ;
        if( std::ifstream(path) )
        {
            std::cout << "found\n" ;
            good_names.push_back(path) ;
        }
        else std::cout << "not found\n" ;
    }

     return good_names ;
}
Last edited on
Perhaps I was not clear defining my problem.

The problem is not in finding the files,
the problem is in:

strcpy(fName7[i1], fName4)

The end program will have the cout lines removed, these are here just to test what is happening.

When I cout << fName4, it displays the good names: enc_ref.000, etc.
When I cout << fName7[i1] all I get is the surrounding text.

I need an indexable array of valid file names to display (later in the program) for the user to make a choice. I also will be making changes within the base name itself as this program is further developed.

I do not understand the push_back thing and wonder how this is indexable?
I will have to do some study on this vector thing to see if it will do my job for me.

When I first learned programming (Fortran) more than 40 years ago, it was always considered important to have your code written simply so the poor guy who had to look at your code after you could easily understand it. I have always thought in simple terms since, that is one reason I used the inelegant method to increment the file extension.

I will clean up the code prior to final compilation.

I just found out that the information is in my fName7[i1], but cout does not display it, so - -

final code for this portion is:


while (!done1)
{
strcpy (fName7[i1], "enc_ref." );
strcat (fName7[i1], ext1);
ifstream f7 (fName7[i1]);

/* strcpy (fName4, fName7[i1]); // for test purposes only
if (f7.good()) cout << "\n Found file: " << fName4; // test purpose only */

if (f7.good()) i1++;
f7.close();

ext1[2]++; // increment file extension
if (ext1[2] > '9')
{ ext1[2] = '0';
ext1[1]++;
if (ext1[1] > '9')
{ ext1[1] = '0';
ext1[0]++;
if (ext1[0] > '9') done1 = true;
}
} // end file extension
if (i1 >= 99) done1 = true;
} // end while


I apreciate the response, but I still have two problems:
1. How do I properly post code segments?
2. I need a simple way to display fName7[i1] as it does not show with cout <<
Last edited on
Topic archived. No new replies allowed.