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:/?它只在一行中读取,即“”