OPENFILENAME阻止ifstream工作

时间:2014-12-02 18:00:29

标签: c++ mfc

我正在制作一个简单的实用程序来将CSV文件转换为EDL,但我遇到了一个让我烦恼的奇怪错误。我试图获取文件名并打开文件,如果我注释掉第一部分,如下例所示,第二部分可行。如果我运行第一部分,它会成功获取文件名但是甚至没有传递收集的文件名但仍明确说明它为“C:\ santor_4k.csv”,myReadFile.open不会打开文件。 out文件仍然正常,它永远不会进入if(myReadFile.is_open())循环。

void CMFCApplication3Dlg::OnBnClickedButtonOpen()
{

    // TODO: Add your control notification handler code here
    /*OPENFILENAME ofn;       // common dialog box structure
    char szFile[260];       // buffer for file name
    HWND hwnd;              // owner window
    HANDLE hf;              // file handle

    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    //ofn.hwndOwner = hwnd;
    ofn.lpstrFile = szFile;
    // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
    // use the contents of szFile to initialize itself.
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = "CSV\0*.CSV\0";
    //ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

    // Display the Open dialog box. 

    if (GetOpenFileName(&ofn) == TRUE)
        hf = CreateFile(ofn.lpstrFile,
        GENERIC_READ,
        0,
        (LPSECURITY_ATTRIBUTES)NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        (HANDLE)NULL);

    m_EchoText.Format(szFile);*/

    int counter = 0;
    int lastfcc = 0;
    int fcc;
    ifstream myReadFile;
    myReadFile.open("C:\\santor_4k.csv");
    std::ofstream outfile;
    outfile.open("C:\\santor_4k_r3_resolve.edl", std::ofstream::out | std::ofstream::app);
    char output[260];
    //resolve
    cout << "TITLE: ( no title )" << endl << endl;
    outfile << "TITLE: ( no title )" << endl << endl;
    //revival
    //outfile << "TITLE: ( no title )" << endl;
    //outfile << "FCM: NON - DROP FRAME"<<endl << endl;;
    if (myReadFile.is_open()) {
        while (!myReadFile.eof()) {
            int first;
            int second;
            char lastTC[40];
            myReadFile >> output;
            //cout << int(output[0]) << endl;
            if (int(output[0]) > 47 && int(output[0]) < 58)
            {// cout << output << endl; 
                std::string str = output;
                std::size_t found = str.find(",");
                if (found != std::string::npos){
                    //cout <<"first comma" << found << endl; 
                    first = found + 1;
                }
                found = str.find(",", found + 1);
                if (found != std::string::npos) {
                    //cout << "second comma" << found << endl; 
                    second = found - first;
                }
                std::string  fccstring = str.substr(first, second);
                //cout << "fccString=" << fccstring<<endl;
                fcc = stoi(fccstring); //+1 for revival ?
                toTimeCode(lastfcc);
                strcpy_s(lastTC, timecode);
                toTimeCode(fcc);
                cout << setfill('0') << setw(3) << counter << "  001      V     C " << lastTC << " " << timecode << " " << lastTC << " " << timecode << " " << endl << endl;
                //resolve
                if (counter>0)outfile << setfill('0') << setw(3) << counter << "  001      V     C " << lastTC << " " << timecode << " " << lastTC << " " << timecode << " " << endl << endl;
                //revival
                //if (counter>0)outfile << setfill('0') << setw(3) << counter << "   title     V     C " << lastTC << " " << timecode << " " << lastTC << " " << timecode<<"\r";

                lastfcc = fcc;
                counter++;
            }


        }
    }
    myReadFile.close();
    outfile.close();
    //std::cout << "Press ENTER to continue...";
    //std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    UpdateData(FALSE);
}

2 个答案:

答案 0 :(得分:0)

szFile之后,你在GetOpenFileName(&ofn)中得到了什么?在致电memset(szFile, 0, sizeof(szFile));之前,您还应该GetOpenFileName

答案 1 :(得分:0)

我想出了自己的问题,我想你不应该在不了解它的情况下复制一个例子,这一部分:

if(GetOpenFileName(&amp; ofn)== TRUE)         hf = CreateFile(ofn.lpstrFile,         GENERIC_READ,         0,         (LPSECURITY_ATTRIBUTES)NULL,         OPEN_EXISTING,         FILE_ATTRIBUTE_NORMAL,         (HANDLE)NULL);

已打开一个文件,所以当我再次打开它时,它没有工作。通过删除它,我解决了问题