Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.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++ 在C++;,打开一个带有值的.txt文件后,读取该文件会显示“0”&引用;_C++_Ifstream_Readfile - Fatal编程技术网

C++ 在C++;,打开一个带有值的.txt文件后,读取该文件会显示“0”&引用;

C++ 在C++;,打开一个带有值的.txt文件后,读取该文件会显示“0”&引用;,c++,ifstream,readfile,C++,Ifstream,Readfile,这是我的密码 ifstream readFile; readFile.open("voltages.txt"); if (readFile.fail( ) ) { cout << "\nCould not open the file ... check to see if voltages.txt exists\n"; system("pause"); return; } string tempString = ""; //readFile.seekg(0

这是我的密码

ifstream readFile;
readFile.open("voltages.txt");

if (readFile.fail( ) )
{
    cout << "\nCould not open the file ... check to see if voltages.txt exists\n";
    system("pause");
    return;
}

string tempString = "";
//readFile.seekg(0, ios::beg);
int idx = 0;
if (readFile.is_open())
{
    while ( readFile.good() )
    {
        getline(readFile,tempString);
        cout << tempString << endl;
    }
    readFile.close();
}
ifstream读取文件;
打开(“voltages.txt”);
if(readFile.fail())
{

cout这可能是因为当程序需要从C链接到文件时,您正在尝试读取本地文件:/

您可以通过将
voltages.txt
替换为
C:/pathtovities/voltages.txt

我建议通过以下操作将文件传递给可执行文件:

int main(int argc, char argv[]){
    ifstream readFile;
    readFile.open(argv[2]);
    /*Rest of the code*/
这假定文件的路径由您给程序的第一个参数给定,如中所示:

./program pathToFile/voltages.txt

希望这有助于

当voltages.txt不在C:/?它只在一行中读取,即“”