C++ Program Can't Read Text File Written by VB.NET Program

Pat Dotson

New member
Joined
Dec 29, 2012
Messages
1
Programming Experience
1-3
For my C++ program I have a text file that contains user-configurable numeric values. The C++
program creates the text file when it is first run. The text file can be edited with notepad
to change the values, and the C++ program will load the updated values next time it
starts. Everything works as expected here.

The problem...

I created a VB.NET GUI program to read and edit the values from the text file. It works
great. It reads the values then writes changed values back to the text file. The resulting
text file is visually indistinguishable from the original file.

But, after writing the files back to the text file from the GUI, the C++ program will not
read the file. All the values show up with zero values.

Here are snippets of my code:

C++ PROGRAM
********************

VB.NET:
int Range;

//C++ program create/write INI file:

{
      err = fopen_s( &iniFile, "myfile.ini", "r" );
      if( err )
  {
             err = fopen_s( &iniFile, "myfile.ini", "w" );  // try to open it for writing      
             if( err )
                     printf_s( "Cannot create myfile.ini\n" );  // oops      
             else
            { 
                     Range = 44;
                     fseek( iniFile, 0L, SEEK_SET );  // set pointer to beginning of file
                     fprintf_s( iniFile, "%i", Range );
                    fclose( iniFile );
              }    
    }

        //C++ program read INI file:

           else 
           {

                 // read the values from the file
                  fseek( iniFile, 0L, SEEK_SET );  // set pointer to beginning of file
                  fscanf_s( iniFile, "%i", &Range);
                  fclose( iniFile );
                  printf( "%i\n", Range );
            }
}

VB.NET PROGRAM READ/WRITE
*************************

'Read Text File Values:

VB.NET:
Dim LatStrength As Integer

Private Sub ReadCurrentTextFileSettings()

        Dim fileReader As System.IO.StreamReader
        fileReader = _
        My.Computer.FileSystem.OpenTextFileReader(TextFilename)

        LatStrength = fileReader.ReadLine()
        fileReader.Close()

        End Sub

'Write Text File Values:

VB.NET:
Public Sub ApplyCurrentSettingsToTextFile()

        Dim filewriter As System.IO.StreamWriter
        filewriter = My.Computer.FileSystem.OpenTextFileWriter(TextFilename, False)

        filewriter.WriteLine(LatStrength)

        filewriter.Close()

End Sub

*********************************

Things I've tried:

- fscanf instead of fscanf_s
- Various data types; int, double, float, etc.
- Writing directly from the variable as opposed to writing the control.value

Regardless, as soon as I write to the file from VB.NET, the file is no longer readable by the the C++ program.

Anything you can think of that would help is appreciated.
 
Last edited:
I would try opening and then saving the file using VB.NET without making any changes and then comparing the binary contents of the file. Quite possibly the issue is with the encoding used, but you should be able to get a fair idea from the binary data, which would be best viewed as hex.
 
Back
Top