基本C++程序,getline()/parseing a file
Basic C++ program, getline()/parsing a file
我的任务是创建一个小程序,用于解析文本文件并从中获取必要的信息。文件的布局是这样的
Tuesday*Info5051*10:00*11:00*M3039*Info5064*12:00*3:00*G1001;
基本上,它应该将每个字符串存储在结构中,以便我以后可以检索它,但我无法让我的程序工作(我有学习障碍,所以事情往往会变得困难)。这是我到目前为止的代码。(我知道这是一个简单的程序,但我倾向于过度思考/搞砸东西。到目前为止,我遇到的大问题是它无法打开文件以启动。我已将文件保存到bin->debug以及程序的主文件夹中。我确定我错误地使用了 getline 方法。
struct Course
{
string _sDay;
string _sName;
string _sCode;
string _iStart;
string _iDuration;
string _sRoom;
};
int main()
{
ifstream fileIn;
fileIn.open("courseLoad.txt");
vector<Course> vCourse;
string str="*";
string line;
if (!fileIn)
{
cout<<"A error has occured, please contact support.";
}
while(!fileIn.eof())
{
for(int i=0; i!= fileIn.eof();i++)
{
//file.getline(entry.part_num, 6, '-');
getline(fileIn,line,'*');
vCourse[i]._sDay =line;
getline(fileIn,line,'*');
vCourse[i]._sName =line;
getline(fileIn,line,'*');
vCourse[i]._sCode = line;
getline(fileIn,line,'*');
vCourse[i]._iStart =line;
getline(fileIn,line,'*');
vCourse[i]._iDuration = line;
getline(fileIn,line,'*');
vCourse[i]._sRoom =line;
cout<<vCourse[i];
}//end for
}
--output to screen here--
此代码有几个问题:
1) 该代码缺少 return 语句或 else 语句,以防止程序在无法打开文件的情况下继续执行:
if (!fileIn)
{
cout<<"A error has occured, please contact support.";
return -1;
}
2)您的获取线都在同一个输入流上运行。您想在一行中读取,然后解析该行。例如:
// Read in a line
while (getline(fileIn,line))
{
string item;
std::stringstream sstr(line);
// Read in an item
while (getline(sstr, item, "*"))
{
std::cout << item << std::endl;
}
}
3) vCourse 大小为 0,因此不能使用 [] 运算符;但是您可以使用push_back来扩展矢量的大小并在矢量的后面插入一个元素:
// Read in a line
while (getline(fileIn,line))
{
string item;
// Default course construction
Course c;
std::stringstream sstr(line);
// Read in an item
getline(sstr,item,'*');
c._sDay = item;
getline(sstr,item,'*');
c._sName = item;
getline(sstr,item,'*');
c._sCode = item;
getline(sstr,item,'*');
c._iStart = item;
getline(sstr,item,'*');
c._iDuration = item;
getline(sstr,item,'*');
c._sRoom = item;
// Save the course into the vector
vCourse.push_back(c);
}
您还可以在上面添加更多的错误检查(以防行中缺少某些元素)。
一个显而易见的直接问题是,您实际上并没有将任何Course
结构添加到向量中,而是像向量一样分配给它们的元素。例如
vCourse[i]._sDay =line;
但是您实际上并没有在索引 i 处向量添加 Course
结构的缩存。这意味着您分配给不存在的实例,这绝不是好消息。在此之前,您需要的是
Course newItem; // make a new Course object instance
vCourse.push_back(newItem); // This adds the instance to the end of the vector
// Now assign to the members of vCourse[i];
vCourse[i]._sDay =line;
getline(fileIn,line,'*');
vCourse[i]._sName =line;
getline(fileIn,line,'*');
vCourse[i]._sCode = line;
getline(fileIn,line,'*');
vCourse[i]._iStart =line;
getline(fileIn,line,'*');
vCourse[i]._iDuration = line;
getline(fileIn,line,'*');
然后,您可以分配给结构。
另外,如果您想这样做
cout<<vCourse[i];
您将需要重载operator<<
如果您无法打开文件,则需要检查您是否 1) 正确拼写了文件名,2) 该文件是否与可执行文件位于同一位置。无论如何写完整路径名可能会更安全
你也可以尝试将文件的内容放入单个字符串并使用strtok()函数。
相关文章:
- Seg Fault Issue C++ (file IO / getline)
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- File.cpp.o:OpenPose 标志 CMakeFiles/.. 的多重定义/main.cpp.o:首先在这里定
- Visual Studio C++ Project File CustomBuild Task: Filter Outp
- 已解决 - C++ 的崇高文本中的"fatal error: opencv: no such file or directory"
- 可视化 如何在C++中将字符数组转换为 FILE 类型
- 如何在从文件中读取整数时使用 file.eof()?
- "No such file or directory" C++标头问题
- 使用 sftp_open() 使用 C++ 中的 SFTP libssh 将文件从本地复制到远程时,File 返回 N
- Visual Studio "fatal error LNK1104: cannot open file"未使用或包含的库
- typedef vector size_type in header file
- make 命令创建 .file,但不创建应用程序文件
- 为什么 VScode 在 .h 文件中显示"'iostream' file not found"?
- 我在 #include < bits/stdc++h.> 放置时出错"No such file or directory"
- 使用 编译时。RES File with MinGW G++, .无法识别 RES
- fopen 在创建大量文件后因 EFBIG "File too large" 而失败
- 叮叮当当。target_include_directories "No such file or directory"
- File to std::string_view
- 基本C++程序,getline()/parseing a file