我已經在 Visual Studio 2022 中為這個痛苦的程式作業了幾個小時,并且一直在努力弄清楚為什么每次運行它時它都會洗掉不同檔案中的文本。
供參考:對于我的 C 相關作業,我必須設計一個程式來打開一個檔案(在本例中為 age.txt),并顯示其中的資料(應該是 25)。
這是我寫的代碼,供參考:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
ifstream din;
ofstream dout;
string inpFileName;
string age; // The user's age
// Prompt for the age:
cout << "Please provide the file containing your age" << endl << endl;
cin >> inpFileName;
din.open(inpFileName.c_str()); // File extension is arbitrary
dout.open("age.txt"); //
if (din)
{
// Read the age:
getline(din, age);
din >> age;
// Output the age
cout << "Your age is: " << age << " years." << endl;
}
else
cout << inpFileName << " was not found. " << endl;
din.close();
dout.close();
system("pause");
return 0;
}
奇怪的是,它似乎在大多數情況下都可以正常作業,因為它提示我輸入一個檔案,我在其中插入 age.txt。但是,每次我嘗試在其中插入檔案時,這是我得到的輸出:
請提供包含您年齡的檔案
age.txt 你的年齡是:歲。按任意鍵繼續 。. .
看起來,每次我運行程式時,它都會從 age.txt 中洗掉數字“25”,然后繼續給我一個空白結果。
有人知道是什么原因造成的嗎?如果可能的話,我可以使用什么解決方案來阻止我的 C 程式洗掉附件中的文本?
在這里,我為了解決這個問題而嘗試做的一些事情包括:
- 修改 if/else 陳述句(無效) - 修改 cout/dout 陳述句(無效) - 在我的腳本和 age.txt 中洗掉和調整檔案行(無效) - 調整屬性字串(似乎不起作用?沒有得到預期的輸出)。- 在嘗試獲得結果時使用#include 陳述句(也沒有運氣) - 調整我的計算機上的管理設定,看看它是否阻止了任何東西(沒有運氣)
在其他嘗試的修復中,沒有任何運氣。我對此感到完全迷失。(作為參考,我使用 Visual Studio 2022 撰寫此腳本)
uj5u.com熱心網友回復:
首先,一個小代碼審查:
#include<iostream> // Poor spacing
#include<string>
#include<fstream>
using namespace std; // Bad practice
int main() // I'm noticing that a lot of your lines all have an extra space at the end
{
ifstream din; // Don't front-load your declarations; declare when you need them.
ofstream dout; // As described, you don't need this at all.
string inpFileName;
string age; // The user's age // Isn't an age an integer?
// Prompt for the age:
cout << "Please provide the file containing your age" << endl << endl;
cin >> inpFileName;
// Conversion to a C-string has not been necessary for about a decade.
din.open(inpFileName.c_str()); // File extension is arbitrary
dout.open("age.txt"); //
// Better to check for the error up front.
if (din)
{
// Read the age:
getline(din, age);
din >> age;
// Output the age
cout << "Your age is: " << age << " years." << endl;
}
else // Poor formatting
cout << inpFileName << " was not found. " << endl;
din.close();
dout.close();
system("pause"); // Lots of garbage whitespace on this line.
return 0;
}
最大的問題是額外的輸出檔案流。默認情況下,輸出檔案流會擦除它打開的檔案。根據提供的描述,您甚至不需要一個。擺脫它將是修復代碼所需的最少的事情。
而且,雖然我完全理解這可能超出您所學的范圍,但將檔案名作為引數傳遞給main()會好得多。
#include <fstream>
#include <iostream>
int main(int argc, char** argv) {
if (argc != 2) {
std::cerr << "Need a file name.\n";
return 1;
}
std::ifstream din(argv[1]);
if (!din) {
std::cerr << "Error opening file.\n";
return 2;
}
int age;
din >> age;
std::cout << "Your age is: " << age << '\n';
din.close(); // Technically not necessary; the OS will close the file since
// the program is ending anyway, but consider it a best
// practice to handle your business yourself. You'll learn
// ways to automate this later on.
}
輸出:
? ./a.out age.txt
Your age is: 25
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/522171.html
標籤:C 视觉-C
下一篇:按日期列對2D向量進行排序
