C ++从txt文件中读取getline并存储到字符串数组中
C++ Read getline from txt file and store into string array
我正在使用 (getline(MyFile, line)) 从文本文件中读取文本文件,并希望将其存储在每个元素的字符串数组中。
我的代码无法正常工作。
基本上,我需要从一个文件中读取,将每一行保存到一个字符串数组中,然后遍历每个元素的每个字符串。
for 循环也没有增加到 4,我不确定为什么。请帮忙。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int array[16], i;
fstream myFile;
string filename, line, str[256];
int main() {
// 1. The user will enter the full name of the file
cout << "Enter the file name to open: ";
cin >> filename;
getchar(); // clear the input buffer before reading the filename
// 2. Open the file for reading (input)
myFile.open(filename.c_str(), fstream::in);
// only continue if the file has opened successfully
if (myFile.is_open() == false) {
cout << "ERROR: not able to open " << filename << endl;
}
else {
// 3. Access the file - read all the lines from the file
for (i = 0; i < 5; i++) {
while (getline(myFile, line)) {
str[i] = line;
cout << "String [" << i << "] = " << str[i] << endl;
}
}
}
}
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
// get used to not typing, using namespace
int main()
{
std::fstream myFile;
std::string filename;
std::string line;
std::vector<std::string> lines; // use vector instead of arrays
// error handling in C++, is exceptions
myFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
// 1. The user will enter the full name of the file
std::cout << "Enter the file name to open: ";
std::cin >> filename;
//getchar(); // clear the input buffer before reading the filename
// 2. Open the file for reading (input)
try
{
myFile.open(filename.c_str(), std::fstream::in);
// 3. Access the file - read all the lines from the file
int count = 0;
while (getline(myFile, line))
{
lines.push_back(line);
std::cout << "String [" << count++ << "] = " << line << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "ERROR: not able to open " << filename << " error = " << e.what() << std::endl;
}
}
我对您的代码进行了 2 处更改以读取和打印文本文件的所有行(<256 行)
1.comment 出 for
语句
2.insert i++
在 while 循环中
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int array[16], i;
fstream myFile;
string filename, line, str[256];
int main() {
// 1. The user will enter the full name of the file
cout << "Enter the file name to open: ";
cin >> filename;
getchar(); // clear the input buffer before reading the filename
// 2. Open the file for reading (input)
myFile.open(filename.c_str(), fstream::in);
// only continue if the file has opened successfully
if (myFile.is_open() == false) {
cout << "ERROR: not able to open " << filename << endl;
}
else {
// 3. Access the file - read all the lines from the file
//for (i = 0; i < 5; i++)
{
while (getline(myFile, line)) {
str[i] = line;
cout << "String [" << i << "] = " << str[i] << endl;
i++;
}
}
}
}
我正在使用 (getline(MyFile, line)) 从文本文件中读取文本文件,并希望将其存储在每个元素的字符串数组中。 我的代码无法正常工作。 基本上,我需要从一个文件中读取,将每一行保存到一个字符串数组中,然后遍历每个元素的每个字符串。 for 循环也没有增加到 4,我不确定为什么。请帮忙。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int array[16], i;
fstream myFile;
string filename, line, str[256];
int main() {
// 1. The user will enter the full name of the file
cout << "Enter the file name to open: ";
cin >> filename;
getchar(); // clear the input buffer before reading the filename
// 2. Open the file for reading (input)
myFile.open(filename.c_str(), fstream::in);
// only continue if the file has opened successfully
if (myFile.is_open() == false) {
cout << "ERROR: not able to open " << filename << endl;
}
else {
// 3. Access the file - read all the lines from the file
for (i = 0; i < 5; i++) {
while (getline(myFile, line)) {
str[i] = line;
cout << "String [" << i << "] = " << str[i] << endl;
}
}
}
}
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
// get used to not typing, using namespace
int main()
{
std::fstream myFile;
std::string filename;
std::string line;
std::vector<std::string> lines; // use vector instead of arrays
// error handling in C++, is exceptions
myFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
// 1. The user will enter the full name of the file
std::cout << "Enter the file name to open: ";
std::cin >> filename;
//getchar(); // clear the input buffer before reading the filename
// 2. Open the file for reading (input)
try
{
myFile.open(filename.c_str(), std::fstream::in);
// 3. Access the file - read all the lines from the file
int count = 0;
while (getline(myFile, line))
{
lines.push_back(line);
std::cout << "String [" << count++ << "] = " << line << std::endl;
}
}
catch (const std::exception& e)
{
std::cout << "ERROR: not able to open " << filename << " error = " << e.what() << std::endl;
}
}
我对您的代码进行了 2 处更改以读取和打印文本文件的所有行(<256 行)
1.comment 出 for
语句
2.insert i++
在 while 循环中
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int array[16], i;
fstream myFile;
string filename, line, str[256];
int main() {
// 1. The user will enter the full name of the file
cout << "Enter the file name to open: ";
cin >> filename;
getchar(); // clear the input buffer before reading the filename
// 2. Open the file for reading (input)
myFile.open(filename.c_str(), fstream::in);
// only continue if the file has opened successfully
if (myFile.is_open() == false) {
cout << "ERROR: not able to open " << filename << endl;
}
else {
// 3. Access the file - read all the lines from the file
//for (i = 0; i < 5; i++)
{
while (getline(myFile, line)) {
str[i] = line;
cout << "String [" << i << "] = " << str[i] << endl;
i++;
}
}
}
}