我刚刚在这个答案中发现了一条评论说iostream::eof在循环条件下使用"几乎肯定是错误的".我通常使用类似的东西while(cin>>n)- 我猜是隐式检查EOF,为什么检查eof显式使用while (!cin.eof())错误?
它与scanf("...",...)!=EOF在C中使用有何不同(我经常使用没有问题)?
我有一个应该从文件中逐行读取的函数,当一行不以'>'或''开头时,读取停止.它应该将行存储在vector中并返回它.
这是代码:
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdio.h>
#include <fstream>
#include <vector>
using namespace std;
string getseq(char * db_file) // gets sequences from file
{
string seqdb;
vector<string> seqs;
ifstream ifs(db_file);
string line;
//vector<char> seqs[size/3];
while(ifs.good())
{
getline(ifs, seqdb);
if (seqdb[0] != '>' & seqdb[0]!=' ')
{
seqs.push_back(seqdb);
}
}
ifs.close();
//return seqs;
//return seqs;
}
int main(int argc, char * argv[1])
{
cout << "Sequences: \n" << getseq(argv[1]) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器(g ++)返回:
fasta_parser.cpp: In …Run Code Online (Sandbox Code Playgroud) 我理解cin.eof()测试流格式.在输入时,输入错误时不会到达字符结尾.我在MSV C++ 2010上对此进行了测试,并没有理解奇怪的结果.无论我给出什么输入,我都会收到程序中存在的格式错误消息.
#include <iostream>
using namespace std;
int main()
{
int i;
cin>> i;
if(!cin.eof())
{
cout<< "\n Format Error \n";
}
else
{
cout<< "\n Correct Input \n";
}
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果我预计:
i =的值
有人可以解释我哪里错了.谢谢.
我正在忙于项目欧拉,我正在努力解决问题11,但答案似乎是错误的.但我不明白为什么,代码似乎是正确的.
vector<vector<int> > grid;
ifstream stream("/home/uauser/workspace/Project_euler/grid.txt");
string line;
char *tok;
if (stream.is_open())
{
while(stream.good())
{
getline(stream, line);
tok = strtok((char *)line.c_str(), " ");
vector<int> row;
while (tok != NULL)
{
int field;
stringstream ss;
ss << tok;
ss >> field;
row.push_back(field);
tok = strtok(NULL, " ");
}
grid.push_back(row);
}
stream.close();
}
int product = 0;
for(unsigned int i = 0; i < grid.size(); i++)
{
for(unsigned int j = 0; j < grid.at(i).size(); j++)
{
if( i < …Run Code Online (Sandbox Code Playgroud) 我试图从文件中读取客户的姓名,身份证和贷款信息.该文件设置如下:
Williams, Bill
567382910
380.86
Davidson, Chad
435435435
400.00
Run Code Online (Sandbox Code Playgroud)
基本上,每次我使用新名称时,信息都将被放入Customer类的新对象中.我的问题是,我正在尝试从文件中读取,但我不确定如何正确地重载操作符,只读取文件中的3行,并将它们放在正确的位置.
我在这里创建客户并打开文件:
Menu::Menu()
{
Customer C;
ifstream myFile;
myFile.open("customer.txt");
while (myFile.good())
{
myFile >> C;
custList.insertList(C);
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我在.cpp文件中的Menu类.以下是我的.cpp文件中Customer类的重载运算符的代码(我知道该怎么做).
istream& operator >> (istream& is, const Customer& cust)
{
}
Run Code Online (Sandbox Code Playgroud)
我不确定如何只获得三条线并将它们放入客户的各自位置:
string name
string id
float loanamount
Run Code Online (Sandbox Code Playgroud)
如果有人能帮我解决这个问题,我真的很感激.
我有这个程序
int main()
{
string valami = "-- .- .-. -.- ------ -- .- .-. -.-";
from_morse_string(valami);
return 0;
}
int from_morse_string(string input_morse_string)
{
string morse_arr[1764];
int j = 0;
stringstream ssin(input_morse_string);
while (ssin.good() && j < 1764)
{
ssin >> morse_arr[j];
++j;
}
for(int i = 0; i < j; i++)
{
switch(morse_arr[i])
{
case ".-" : cout << "a" << endl; break;
case "-..." : cout << "b" << endl; break;
case "-.-." : cout << "c" << …Run Code Online (Sandbox Code Playgroud) 这就是我到目前为止所做的:我想从C++中读取文件中的单词,并且我只允许使用cstring库.这是我的一段代码
#include <cstring>
#include <fstream>
#include <stdio.h>
using namespace std;
int main(){
ifstream file;
char word[1];
file.open("p.txt");
while (!file.eof()){
file >> word;
cout << word << endl;
}
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,一次只读一个字.但我不明白这是如何工作的.任何大小的char数组怎么可能是char字[1]或char字[50]一次只读一个字而忽略空格.
而且我想将这些单词存储在动态数组中.我怎样才能做到这一点?任何指导将不胜感激?
我试图通过c ++读取一个巨大的txt.它有70mb.我的目标是逐行子串并生成另一个只包含我需要的信息的较小的txt.
我得到下面的代码来读取文件.它适用于较小的文件,但不适用于70mb怪物.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream myReadFile;
myReadFile.open("C:/Users/Lucas/Documents/apps/COTAHIST_A2010.txt");
char output[100];
if (myReadFile.is_open()) {
while (myReadFile.eof()!=1) {
myReadFile >> output;
cout<<output;
cout<<"\n";
}
}
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:SeparadorDeAcoes.exe中0x50c819bc(msvcp100d.dll)的未处理异常:0xC0000005:访问冲突读取位置0x3a70fcbc.
如果有人可以用C或甚至C#指出解决方案,那也是可以接受的!
谢谢=)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile("hey.txt");
myfile >> line;
cout << line;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么这不打印出我的“hey.txt”文件中的内容?