locked
stat and fopen problems RRS feed

  • Question

  • Hello all,

    Migrating code from VC 6 to Visual Studio 2008 SP1.  Got some wierdness that I cannot figure out.  Something has changed such that this simple code no longer works.  

    //int i = stat ( mdfile[tickerId].c_str(), &buf );
        int i = stat ( "E:/ADE/Data/MDELTA_6EZ8_1min2.CSV", &buf );
            
        if ( i != 0 ) {
         fp=fopen("E:/ADE/Data/MDELTA_6EZ8_1min2.CSV", "a+");  
         //fp=fopen(mdfile[tickerId].c_str(),"a+");  //cj

    Pretty simple right?  The commented out code used to work.  In fact in debugging the value is there as in the hard coded line below.  HOWEVER it does not work!!  The stat function will no longer see that file is there and the fopen will not append to the file UNLESS it is hard coded in.  Unfortunately I'm saving to about 10 different files so cannot hard code.

    ANY HELP as to what's changed would be greatly appreciated!!

    Thanks!
    bigboy12
    Friday, November 7, 2008 7:12 PM

Answers

  • More context is necessary to answer that question. What type is mdfile? std::string? Also, as you haven't actually said yet, what exact problem are you encountering? Compiler errors? Runtime errors?
    • Marked as answer by jack 321 Thursday, November 13, 2008 2:20 AM
    Friday, November 7, 2008 9:31 PM
  •  Pretty strange. I tried this code:

    int i;

    std::string mdfile[3];

    FILE* testFile;

    mdfile[0] = "C:\\Projects\\CharTest\\TestFiles\\1.txt";

    mdfile[1] = "C:\\Projects\\CharTest\\TestFiles\\2.txt";

    mdfile[2] = "C:\\Projects\\CharTest\\TestFiles\\3.txt";

    for (i = 0; i < 3; i++)

    {

    testFile = fopen(mdfile[i].c_str(), "a+");

    if (testFile == NULL)

    {

    puts("Error while opening file!");

    }

    else

    {

    printf("File %s was opened.\n", mdfile[i].c_str());

    fclose(testFile);

    }

    }

    getchar();

    return 0;
    And everything was OK, files were opened without any errors. So I have 3 hypotheses:
    1) File that you are trying to open doesn't exist;
    2) tickerId variable are going out of array bounds;
    3) Some errors while forming mdfile array (you'd better post code here).

    • Marked as answer by jack 321 Thursday, November 13, 2008 2:21 AM
    Saturday, November 8, 2008 12:36 AM
  • First off, note that you have used a single backslash in two of
    the file name strings in your example:

    fp5 = fopen("C:\contzf.txt", "r");
    fp=fopen("C:\mdeltadlllogzf.txt", "a+");

    They should be doubled:

    fp5 = fopen("C:\\contzf.txt", "r");
    fp=fopen("C:\\mdeltadlllogzf.txt", "a+");

    Secondly, the problem you are having with stat and fopen when using
    the string object to get the file name via c_str() is due to the presence
    of a leading space character in the filename string (before the drive letter).
    Your hardcoded examples:

    //i = stat ( "C:/MDELTA_6EZ8_1min.CSV", &buf );  // Works!
    fp=fopen("C:/MDELTA_6EZ8_1min.CSV", "a+");  //works!!

    will also fail if you put a leading space character in the strings:

    //i = stat ( " C:/MDELTA_6EZ8_1min.CSV", &buf );
    fp=fopen(" C:/MDELTA_6EZ8_1min.CSV", "a+");

    Solution: Correct your construction of adefilebuf by removing the
    leading space.

    - Wayne

    • Marked as answer by bigboy12 Sunday, November 16, 2008 12:53 AM
    Saturday, November 15, 2008 8:00 PM
  • Quote>how do I get rid of the leading space?

    By parsing the string.

    Various approaches:

    (1) If you are *certain* that it will *always* be there, you can simply skip
    it in your fscanf by adding %*c at the appropriate place:
    number=fscanf(fp5,"%i%s%i%s%i%*c%[^\n]",
       &tmptickerId,symbuf,&multbuf,excbuf,&tsbuf,adefilebuf);

    (2) Or you can strip it from the string object later:
    mdfile[tmptickerId] = (const char*)adefilebuf;
    if(mdfile[tmptickerId][0] == ' ')  mdfile[tmptickerId].erase(0, 1);

    You can iterate to remove multiple leading spaces, if any.

    etc.

    - Wayne



    • Marked as answer by bigboy12 Sunday, November 16, 2008 12:53 AM
    Saturday, November 15, 2008 8:59 PM

All replies

  • I suspect your project settings are set to use Unicode character set. Either change the CRT calls (e.g. fopen) to Unicode functions (e.g. _wfopen) or better yet character-encoding agnostic functions (e.g. _tfopen), or change the character set in your project settings from Unicode to MBCS or Not Set.
    Friday, November 7, 2008 7:40 PM
  • HI ildjarn,

    You may be up to something here.  Been trying to do various options.  The original file had the slashed the other way - worked for over a year in Visual C 6.  I noticed that the backslashes \ however did not work at all in Visual Studio 2008 so switched them all to forward slashes /.

    Maybe you could explain this to me. 

    thanks so much!
    bigboy12

    Friday, November 7, 2008 8:40 PM
  • In C++, backslash begins an escape sequence. Since you want an actual backslash character, you need two of them (i.e. \\) to represent the escape sequence for a literal backslash.

    In my opinion, stick with forward slashes for path separators; they work fine.
    Friday, November 7, 2008 8:49 PM
  • OK,

    Well the hair is starting to come out.  I've tried setting to "not set" and Multi byte but neither worked.  Is there an alternative to this?

         fp=fopen(mdfile[tickerId].c_str(),"a+"); 

    I read the lines in then need to put them into an array of strings.   Any ideas??  I've got the include <iostream.h> as well so this is there.   Seems like there is something missing.

    bigboy12 
    Friday, November 7, 2008 9:26 PM
  • More context is necessary to answer that question. What type is mdfile? std::string? Also, as you haven't actually said yet, what exact problem are you encountering? Compiler errors? Runtime errors?
    • Marked as answer by jack 321 Thursday, November 13, 2008 2:20 AM
    Friday, November 7, 2008 9:31 PM
  • mdfile is std::string  runtime error occurs where it can't find the file nor can it open it and program crashes.  When debugging the char array in the mdfile is EXACTLY the same as when I'm hard coding it in.

    Thanks!!
    Friday, November 7, 2008 10:03 PM
  • By the way, it will do this with the char array variable as well as the std::string so it doesn't matter.  It's like fopen and struct stat don't work in the new SP1 visual C stuff.
    Friday, November 7, 2008 10:05 PM
  •  Pretty strange. I tried this code:

    int i;

    std::string mdfile[3];

    FILE* testFile;

    mdfile[0] = "C:\\Projects\\CharTest\\TestFiles\\1.txt";

    mdfile[1] = "C:\\Projects\\CharTest\\TestFiles\\2.txt";

    mdfile[2] = "C:\\Projects\\CharTest\\TestFiles\\3.txt";

    for (i = 0; i < 3; i++)

    {

    testFile = fopen(mdfile[i].c_str(), "a+");

    if (testFile == NULL)

    {

    puts("Error while opening file!");

    }

    else

    {

    printf("File %s was opened.\n", mdfile[i].c_str());

    fclose(testFile);

    }

    }

    getchar();

    return 0;
    And everything was OK, files were opened without any errors. So I have 3 hypotheses:
    1) File that you are trying to open doesn't exist;
    2) tickerId variable are going out of array bounds;
    3) Some errors while forming mdfile array (you'd better post code here).

    • Marked as answer by jack 321 Thursday, November 13, 2008 2:21 AM
    Saturday, November 8, 2008 12:36 AM
  • post  the exact error messages you're getting. I had similar problems migrating code from VS2005 to VS2008. All that was needed was to remove a particular library reference from the project to get it done. Post the libraries listed in the project settings in addition. 
    • Marked as answer by jack 321 Thursday, November 13, 2008 2:21 AM
    • Unmarked as answer by bigboy12 Saturday, November 15, 2008 5:26 PM
    Sunday, November 9, 2008 12:11 AM
  • Ok,

    Sorry took so long to get back, but I decided to try an a different computer and the results are the same with both standard Visual Studio 2008SP1 and Express Studio 2008 SP1.  Here's the problem code stripped down.  Notice the comments of Doesn't Work vs Works.  The Doesn't work code always worked in VC 6.  I think I've found a bug in VStudio 2008.  To replicate this simply create a new non mfc non atl window32 simple console app and paste in the code below.  Not sure if I can attach the project file to this or not.

    // testfile_stat.cpp : Defines the entry point for the console application.  
    //  
     
    #include "stdafx.h"  
    #include <sys/stat.h>  // for file status checking  
    #include <stdio.h> 
    #include <string> 
     
    int tmptickerId = 0;  
    std::string mdfile[20];  
     
    int _tmain(int argc, _TCHAR* argv[])  
    {  
        static FILE *fp, *fp2, *fp5;   
        int number = 0;  
        fp5 = fopen("C:\contzf.txt", "r");  
          
        if (fp5 != NULL)  
        {     
            while (number != EOF)  
            {  
              
            int multbuf = 0;  
              
            double tsbuf = 0.0;  
            char symbuf[50], excbuf[50], adefilebuf[50];  
              
              
            number=fscanf(fp5,"%i%s%i%s%i%[^\n]",&tmptickerId,symbuf,&multbuf,excbuf,&tsbuf,adefilebuf);  
              
            if (number == 6)   
                {  
                      
                    mdfile[tmptickerId] = (const char*)adefilebuf;  
                      
                    //Sleep(1000);  
                }  
            else if (number == EOF)  
                {  
                fclose(fp5);  
                }  
            }  
            fclose(fp5);  
        }  
     
        struct stat buf;  
        int i = 0;  
        i = stat ( mdfile[tmptickerId].c_str(), &buf );  //Doesn't Work!  
        //i = stat ( "C:/MDELTA_6EZ8_1min.CSV", &buf );  // Works!  
     
        //debug print below  
        fp=fopen("C:\mdeltadlllogzf.txt", "a+");  
        fprintf (fp, "i = %i mdfile = %s\n",i, mdfile[tmptickerId].c_str());  
        fclose(fp);  
        //end of debug print  
     
        if ( i != 0 )   
        {  
            //fp=fopen(mdfile[tmptickerId].c_str(), "a+");  //Doesn't Work!!  
            fp=fopen("C:/MDELTA_6EZ8_1min.CSV", "a+");  //works!!  
     
            // debug print below  
            /*  
            fp2=fopen("C:/mdeltadlllogzf.txt", "a+");  
            fprintf(fp2, "fp = %i mdfile = %s\n",fp, mdfile[tmptickerId].c_str());  
            fclose(fp2);  
            //end of debug print  
            */  
                          
            fprintf(fp,"Date,Time,mdhigh,mdlow,index1\n");  
            fclose(fp);  
        }             
     
        return 0;  
    }  
     
     
    Now simply put this line of code into a simple text file and name it contzf.txt and put it in the C directory on your computer.
    1 6EZ8 10000 CME 1 C:/MDELTA_6EZ8_1min.CSV 

    Put a debug and watch the integer i.  After you run this once, you'll get the .csv file in your c director.  After you run it the second time it will write to it again instead of skipping it because i will always = -1 even though the file is there.  It's not really a stat problem because the commented out code will work.  The fopen only works with the hard coded file in there as well.  This is a MAJOR bug me thinks.  By the way it used to work no matter which way the slashed worked forward or backward.  But now either way it will not work reading the file in and using .c_str().

    Will look forward to your replies.  Oh yes, the libraries currently used are:

     kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

    Thanks!

    Bigboy

    Saturday, November 15, 2008 5:50 PM
  •  I decided to run a verbose option and see what Libraries it was choosing.  Here's the results below:

       Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\kernel32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\user32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\gdi32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\winspool.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\comdlg32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\advapi32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\shell32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\ole32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\oleaut32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\uuid.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\odbc32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\odbccp32.lib:
        Searching H:\Program Files\Microsoft Visual Studio 9.0\VC\lib\MSVCRTD.lib:
        Searching H:\Program Files\Microsoft Visual Studio 9.0\VC\lib\OLDNAMES.lib:
        Searching H:\Program Files\Microsoft Visual Studio 9.0\VC\lib\msvcprtd.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\kernel32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\user32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\gdi32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\winspool.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\comdlg32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\advapi32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\shell32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\ole32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\oleaut32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\uuid.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\odbc32.lib:
        Searching H:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib\odbccp32.lib:
        Searching H:\Program Files\Microsoft Visual Studio 9.0\VC\lib\MSVCRTD.lib:
        Searching H:\Program Files\Microsoft Visual Studio 9.0\VC\lib\OLDNAMES.lib:
        Searching H:\Program Files\Microsoft Visual Studio 9.0\VC\lib\msvcprtd.lib:

    Don't know if this helps but wanted to make sure no stone is left unturned!
    Bigboy12
    Saturday, November 15, 2008 6:24 PM
  • First off, note that you have used a single backslash in two of
    the file name strings in your example:

    fp5 = fopen("C:\contzf.txt", "r");
    fp=fopen("C:\mdeltadlllogzf.txt", "a+");

    They should be doubled:

    fp5 = fopen("C:\\contzf.txt", "r");
    fp=fopen("C:\\mdeltadlllogzf.txt", "a+");

    Secondly, the problem you are having with stat and fopen when using
    the string object to get the file name via c_str() is due to the presence
    of a leading space character in the filename string (before the drive letter).
    Your hardcoded examples:

    //i = stat ( "C:/MDELTA_6EZ8_1min.CSV", &buf );  // Works!
    fp=fopen("C:/MDELTA_6EZ8_1min.CSV", "a+");  //works!!

    will also fail if you put a leading space character in the strings:

    //i = stat ( " C:/MDELTA_6EZ8_1min.CSV", &buf );
    fp=fopen(" C:/MDELTA_6EZ8_1min.CSV", "a+");

    Solution: Correct your construction of adefilebuf by removing the
    leading space.

    - Wayne

    • Marked as answer by bigboy12 Sunday, November 16, 2008 12:53 AM
    Saturday, November 15, 2008 8:00 PM
  • Wayne,

    Wow, ok how do I get rid of the leading space??
    bigboy12
    Saturday, November 15, 2008 8:42 PM
  • Quote>how do I get rid of the leading space?

    By parsing the string.

    Various approaches:

    (1) If you are *certain* that it will *always* be there, you can simply skip
    it in your fscanf by adding %*c at the appropriate place:
    number=fscanf(fp5,"%i%s%i%s%i%*c%[^\n]",
       &tmptickerId,symbuf,&multbuf,excbuf,&tsbuf,adefilebuf);

    (2) Or you can strip it from the string object later:
    mdfile[tmptickerId] = (const char*)adefilebuf;
    if(mdfile[tmptickerId][0] == ' ')  mdfile[tmptickerId].erase(0, 1);

    You can iterate to remove multiple leading spaces, if any.

    etc.

    - Wayne



    • Marked as answer by bigboy12 Sunday, November 16, 2008 12:53 AM
    Saturday, November 15, 2008 8:59 PM
  • Wayne,


    THanks a ton, after this hurdle there were some smaller things in upgrading to VS 2008 but it looks like it is all now working!!   Missed that big time.  Looking at the debugger I though the two single quotes with an empty space in between were 1 double quote!.  Wasn't thinking I shouldn't have a quote at all in a character array!!

    Thanks again!

    bigboy12
    Sunday, November 16, 2008 12:55 AM