Frage

I've been stuck on this issue for a while. What I have here is a loop that will read a text file containing file names. The loop reads these lines one by one, and sets it into memory via the variable sFileName. sFileName is later called upon to load an image, and the program does this one by one as the loop loads it. The user then selects a tag for the image and loads the next one. The tag and the image file name are exported into another text file, named imgresults.txt. Now, the text file with the file names is a few thousand lines. So, in the case that the user has to exit the program and tries to continue later, the loop restarts, instead of leaving off at the point when the program was closed.

I am trying to find a way to have the loop start at that point. So far, I decided to use getline() to count how many lines are currently in imgresults.txt, as that will give the number of images that have already been run through the program. This number is stored in the variable "x". I've been doing a lot of research, but I just cannot find how to set a condition for the while loop to begin at line "x". Do you guys have any suggestions? Also, if you need any clarifications, please ask. I only included the code regarding the loop, as the code for loading the image and such is perfect fine.

int _tmain(int argc, _TCHAR* argv[])
{

    int value = 0;

int nCounter = 0;
FILE* fIn = NULL;
char * sLine = new char[MAX_FILENAME_SIZE];
char * sFileName = new char [MAX_FILENAME_SIZE];
char * s = new char [MAX_FILENAME_SIZE];

#define ImgListFileName "path"
#define ImgRepository "path"



if ((fIn = fopen(ImgListFileName,"rt"))==NULL)

{
    printf("Failed to open file: %s\n",ImgListFileName);
    return nCounter;
}



ifstream imgresults;
    imgresults.open ("imgresults.txt");
    int x=0;
    string line;

    while(!imgresults.eof()) {
        getline (imgresults, line);
        x++;
    }
    srand (time(NULL));
    cout << x;


    while(!feof(fIn)){
        memset(sLine,0,MAX_FILENAME_SIZE*sizeof(char));
        memset(sFileName,0,MAX_FILENAME_SIZE*sizeof(char));
        memset(s,0,MAX_FILENAME_SIZE*sizeof(char));
        fgets(sLine,MAX_FILENAME_SIZE,fIn);
        strncpy(s,sLine,65);
        strcat(sLine,"\0");
        strcat(sFileName,s);


        printf (sFileName);

        nCounter++;
}

Thanks in advance!

War es hilfreich?

Lösung

If you really want to use imgresults.txt as the information on where you should start from the input file, then the best you can do is to have a while loop to read x lines from the input file just before the while loop where you read the input file.

while (x--) {
    fgets(sLine, MAX_FILENAME_SIZE, fIn);
}

Better solution would probably be to write state of processing to another file so that you would not have to read input file line by line, but you could immediately seek to a known offset in the file.

Andere Tipps

Use ifstream::tellg to retrieve and store the current position of the file when the programm was closed.

Use ifstream::seekg to restore that position when the porgramm restarts.

Before you read each line, save the current offsets in your input and output file to a separate file, seeking back to the beginning and overwriting the existing data each time.

When/if you restart, read the offsets from that file, seek to those points, and start working from there.

You can just read lines in parallel from both files and stop when you reach the end of results file. When the loop ends, you have already discarded the file names that were already processed.

ifstream results("results.txt");
ifstream names("names.txt");

if (results && names) {
    std::string temp1, temp2;
    while (getline(results, temp1) && getline(names, temp2)) ; /* do nothing */
}

if (names) {
    // process the rest
}

Not the most efficient solution, but it saves you the hassle of saving offsets. Just make sure that before the very first processing the results file is completely empty (or doesn't exist at all), otherwise this code will skip the first line of names file.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top