正在读取由换行符分隔的文本文件.unix上的C++
Reading a text file that is delimited by a newline. C++ on unix
好的,我在www上尝试了很多不同的建议,并给出了所有的例子。这是一件如此简单的事情,对我来说却不起作用,令人沮丧!我已将需要读取的文件附加到数据结构中。在这个例子中,我只是把它打印到屏幕上。以下代码以前对我有效,但现在不行了。我在visualstudios和unix上都尝试过,但都没有运行它。我最终需要它在unix上运行。
有人能告诉我为什么这不起作用吗,或者建议一种更好的方式来阅读我的文件吗?示例文件:
word
book
programming
这是我读取文件的函数。
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include "words.txt"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream file;
string line;
file.open("words.txt");
if(file.is_open()){
while(getline(file, line)){
getline(file, line);
cout << line <<endl;
}
}
file.close();
return 0;
}
谢谢。
文件,但引发了有关分隔符的错误。例如,在使用g++运行的unix中:
wordlist2.txt: 498841:1: error: missing terminating ' character
incumbent's
^
wordlist2.txt:498875:13 warning: missing terminating ' character [enabled by default]
at your wits' end
^
在视觉工作室:
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 14 error C2146: syntax error : missing ';' before identifier 'mouse'
while (!file.eof())
这不是一个好主意。改为写以下内容:
while (getline(file, line)) {
cout << line << endl;
}
你当然不想
#include "words.txt"
删除此行!!
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何使用 c++ 实现并发文件/文本编辑?
- 我需要转换一些代码,以便它适用于输入和输出文件文本
- 通过套接字发送二进制文件.文本文件有效,其他文件无效
- 将大型数据文件拆分为多个小文件(文本格式)
- 如何读取输入文件(文本文件)并将输入验证为有效整数
- 从文本文件/文本流中读取Q字符串错误
- 从文本文件(文本解析器)C++中读取行和列的逻辑
- 如何将*.css文件(文本文件)的内容与附加信息存储在新文件中?
- 关于内存映射文件和使用大文件文本编辑器
- 如何在读取文件文本时跳过"n"
- 使用libzip从.zip获取文件(文本除外)