C++ 如何跳过每行的前两个字符,将第三个字符分配给c++;
我有一个文本文件如下:C++ 如何跳过每行的前两个字符,将第三个字符分配给c++;,c++,C++,我有一个文本文件如下: 6.1 C 6.2秒 6.3R 6.4 R 这只是前四行。其中90个分为3个部分,共30个部分(6、7和8)。我只想将字符读入数组 这就是我目前所拥有的 #include <iostream> #include <fstream> using namespace std; // Global variables const int MONTHS = 3; const int DAYS = 30; // Function definitions
6.1 C
6.2秒
6.3R
6.4 R
这只是前四行。其中90个分为3个部分,共30个部分(6、7和8)。我只想将字符读入数组 这就是我目前所拥有的
#include <iostream>
#include <fstream>
using namespace std;
// Global variables
const int MONTHS = 3;
const int DAYS = 30;
// Function definitions
void readFile(char daily[][DAYS], int size);
void showStats(char daily[], int days);
int main()
{
char daily[MONTHS][DAYS];
readFile(daily, MONTHS);
// Make sure we place the end message on a new line
cout << endl;
// The following is system dependent. It will only work on Windows
system("PAUSE");
/*
// A non-system dependent method is below
char anyKey;
cout << "Press any key to continue";
cin >> anyKey;
*/
return 0;
}
void readFile(char daily[][DAYS], int size)
{
// open file.
ifstream inputFile("RainOrShine.dat");
if (!inputFile)
{
cout << "ERROR: cannot find/read file." << endl;
exit(EXIT_FAILURE);
}
cout << "Reading file...\n";
// read data.
for (int months = 0; months < size; months++)
{
for (int days = 0; days < DAYS; days++)
{
inputFile >> daily[months][days];
cout << daily[months][days] << ", ";
}
cout << "\nDone with Row[" << (months);
cout << "]...\n";
}
// close file.
inputFile.close();
cout << "Closing File...\n" << endl;
}
void showStats(char daily[], int days)
{
//code
}
#包括
#包括
使用名称空间std;
//全局变量
const int MONTHS=3;
持续时间=30天;
//函数定义
无效读取文件(字符每日[][天],整数大小);
void showStats(字符日[],整数天);
int main()
{
每日[月][日];
readFile(每日,月);
//确保我们将结束消息放在新行上
不能使用任何键;
*/
返回0;
}
无效读取文件(字符每日[][天],整数大小)
{
//打开文件。
ifstream输入文件(“RainOrShine.dat”);
如果(!inputFile)
{
cout如果您只想阅读第三列,可以这样做:
std::ifstream infile("thefile.txt");
int dummy1, dummy2;
char c;
std::vector<char> v;
while (infile >> dummy1 >> dummy2 >> c)
{
std::cout << "We got: '" << c << "'.\n";
v.push_back(c);
}
std::ifstream-infle(“thefile.txt”);
int dummy1,dummy2;
字符c;
std::向量v;
而(填充>>dummy1>>dummy2>>c)
{
std::cout如果您只想阅读第三列,可以执行以下操作:
std::ifstream infile("thefile.txt");
int dummy1, dummy2;
char c;
std::vector<char> v;
while (infile >> dummy1 >> dummy2 >> c)
{
std::cout << "We got: '" << c << "'.\n";
v.push_back(c);
}
std::ifstream-infle(“thefile.txt”);
int dummy1,dummy2;
字符c;
std::向量v;
而(填充>>dummy1>>dummy2>>c)
{
std::cout只需更换
inputFile >> daily[months][days];
借
替换
inputFile >> daily[months][days];
借