Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ <;无法读取内存>;在使用fstream时_C++ - Fatal编程技术网

C++ <;无法读取内存>;在使用fstream时

C++ <;无法读取内存>;在使用fstream时,c++,C++,所以我一直试图打开我创建的一个文件,但当我试图打开我的文件时,我总是遇到一个错误。 下面是调试器所说的: std::basic_ios<char,std::char_traits<char> > = <Unable to read memory> 您是否尝试取出第二个余额文件。打开对账单 我可能还会包括一个控制语句,如if(balanceFile),这样您就知道您正在读取良好的数据。如果使用-g编译,是否会得到错误所在的行号?一些有用的东西: 决定是保持文件

所以我一直试图打开我创建的一个文件,但当我试图打开我的文件时,我总是遇到一个错误。 下面是调试器所说的:

std::basic_ios<char,std::char_traits<char> > = <Unable to read memory>

您是否尝试取出第二个
余额文件。打开
对账单


我可能还会包括一个控制语句,如
if(balanceFile)
,这样您就知道您正在读取良好的数据。如果使用-g编译,是否会得到错误所在的行号?

一些有用的东西:

  • 决定是保持文件打开并仅打开一次,还是多次打开和关闭并每次关闭

  • 修正“balanceiFile”的拼写(为什么要编译?)

  • 存储找到的行数,并检查它是否不超过数组的长度

  • 检查请求的行数是否小于或等于找到的行数

  • int convert(string balance, int lineno){ // Funciton to convert strings in file to ints---------------------------------
    //Getting information from the file about locations
    
    int *pointer;
    pointer = findNewLines();
    static int linenopos[11];
    try{
    
        for (int i = 0; i < 11; i++){
            linenopos[i] = *(pointer + i);
        }
    }
    catch (const std::exception& e){
        std::cout << e.what() << std::endl;
    }
    int balanceInt;
    
    try {
        balanceFile.open("E:\\MoneyStuff\\balance.txt", ios::in | ios::out);
    }
    catch (const std::exception& e) {
        std::cout << e.what() << std::endl;
    }
    
    //Opening file
    balanceFile.open("E:\\MoneyStuff\\balance.txt", ios::in | ios::out);
    
    //Getting Balances
    balanceFile.seekg(linenopos[(lineno - 1)], ios::beg);
    getline(balanceFile, balance);
    
    balanceFile.close();
    
    stringstream convert(balance);//Variable to convert string balance to integer balance
    
    //Converting balance string to int
    convert >> balanceInt;
    
    //Setting balanceInt to 0 if the file doesn't exist
    if (balanceInt < -30000)
        balanceInt = 0;
    
    return balanceInt;
    }
    
    int * findNewLines(){ //Function to find the \n characters in the txt file----------------------------------------------
    //Creating Variables for function
    static int linenopos[11];//Vector to store line positions in
    char c; //Variable for checking character value
    int pos; //Variable for temporarily storing position
    int lineno = 0; //Integer to tell which line to read
    
    //Writing in first line position
    linenopos[0] = 0;
    lineno++;
    
    balanceiFile.open("E:\\MoneyStuff\\balance.txt", ios::binary);
    do{
        balanceiFile.get(c);
        if (c == '\n'){
            pos = balanceiFile.tellg();
            linenopos[lineno] = pos;
            lineno++;
        }
    } while (balanceiFile.good());
    
    //Resetting error flags
    balanceiFile.clear();
    
    return linenopos;
    }