Problem with fopen inside a loop

AB v6.1, afl follows:

Sent this to support today and got an automated response to ask this afl related question here instead.

In words, load the contents of a file into a string variable. Each
line of content is a complete path to another file. Then open each file.

All files are located in the same directory:

The content of "C:\test\FileList.csv" is:

C:\test\1.csv
C:\test\2.csv
C:\test\3.csv

The content of each of these files 1.csv, 2.csv, and 3.csv, is:

line 1
line 2
line 3

The first call to fopen() works correctly. The call to fopen() inside
the loop doesn't work for the first and second loop, but does work on
the third loop. What am I missing?

Many thanks!!

allFiles = "C:\\test\\FileList.csv";

fileListStr = "";

fh0 = fopen( allFiles, "r" );    // <-- WORKS CORRECTLY

if( fh0 )
{
    while( !feof( fh0 ) )
    {
        fileListStr = fileListStr + "," + fgets( fh0 );
    }
    fclose( fh0 );    

    fileListStr = StrMid( fileListStr, 1 );

    for( i = 0; ( filePath = StrExtract( fileListStr, i, ',' ) ) != ""; i++ )
    {   
        fh1 = fopen( filePath, "r" );    // <-- DOES NOT WORK CORRECTLY

        if( fh1 )
        {
            printf( "made it" );
            fclose( fh1 );
        }
    }
}
1 Like

Didi not test, but try

position start is zero-based

Hi awilson,

When ‘fileListStr’ is built in the ‘while’ loop, the first character in the resulting string becomes a comma. The code uses a start position of ‘1’ to trim off this leading comma.

From this:

“,C:\test\1.csv,C:\test\2.csv,C:\test\3.csv” <-- leading comma

to this:

“C:\test\1.csv,C:\test\2.csv,C:\test\3.csv” <-- leading comma removed

You got answer from me. It wasn’t automated.

fopen() is (almost) direct call to C runtime library so it works. If it does not work, you are doing something wrong. In your case the reason is probably very simple - there are characters that you do not see (so called “white spaces”) in the names.

For example, if you have file names each in separate line, fgets() will get the file name together with new line '\n' and/or '\r' (invisible, white character). If you try to pass file name with new line character appended at the end fopen() would fail because there is no such file (with new line character at the end). You would need to trim whitespaces from the end of the line (using StrTrim() function).

Debugging does not hurt. It is all about using debugger and/or TRACE() to find out what is happening in your code.

Now look how much everyone can gain from answering on forum. Now everyone can learn something new.

4 Likes

Ok my mistake.
Your problem is with the new line returned by fgets
The code below works, make sure to include a new line after last line of FileList

allFiles = "C:\\test\\FileList.csv";

fileListStr = "";

fh0 = fopen( allFiles, "r" );    // <-- WORKS CORRECTLY

if( fh0 )
{
    while( !feof( fh0 ) )
    {	
		str = fgets( fh0 );
		len = StrLen(str) - 1; //delete new line char
		if( Len > 0 ) {
			str = StrLeft(str, len);
			fileListStr = fileListStr + "," + str; 
		}
    }
    fclose( fh0 );    
	printf( "\n" );
	printf( fileListStr );
	printf( "\n" );

    fileListStr = StrMid( fileListStr, 1 );
    for( i = 0; ( filePath = StrExtract( fileListStr, i, ',' ) ) != ""; i++ )
    {   
        fh1 = fopen( filePath, "r" );    // <-- DOES NOT WORK CORRECTLY
        printf( "\n" );
        printf( filePath );

        if( fh1 )
        {
            printf( "\nmade it" );
            fclose( fh1 );
        }
    }
}
1 Like

@Tomasz,

Thank you. The automated response I was referring to was the initial response from support containing the ticket number. The fgets documentation does indeed state “fgets reads characters from the current file position to and including the first newline character.” My bad. This makes sense in my situation because the 1st and 2nd lines have a newline character (and were a problem) but the third line does not. The debugger is great. Use it and _TRACE all the time.

@awilson

Great, thanks a lot for the second set of eyes and revised code!!