我想要做的是输入一个文件LINE BY LINE并标记并输出到输出文件中。我有什么能够做到的是输入文件中的第一行,但我的问题是,我无法输入下一行标记,以便它可以保存为输出文件中的第二行,这是我可以做到目前为止在文件中输入第一行。从输入文件逐行输入,使用strtok()和输出到输出文件的标记大小
#include <iostream>
#include<string> //string library
#include<fstream> //I/O stream input and output library
using namespace std;
const int MAX=300; //intialization a constant called MAX for line length
int main()
{
ifstream in; //delcraing instream
ofstream out; //declaring outstream
char oneline[MAX]; //declaring character called oneline with a length MAX
in.open("infile.txt"); //open instream
out.open("outfile.txt"); //opens outstream
while(in)
{
in.getline(oneline,MAX); //get first line in instream
char *ptr; //Declaring a character pointer
ptr = strtok(oneline," ,");
//pointer scans first token in line and removes any delimiters
while(ptr!=NULL)
{
out<<ptr<<" "; //outputs file into copy file
ptr=strtok(NULL," ,");
//pointer moves to second token after first scan is over
}
}
in.close(); //closes in file
out.close(); //closes out file
return 0;
}