私がやろうとしているのは、ファイルをLINE BY LINEで入力し、出力ファイルに出力することです。できたのはファイルの最初の行ですが、私の問題は次の行をトークン化することができないため、出力ファイルに2行目として保存できるようになったことです。ファイル内の最初の行を入力します。入力ファイルから行ごとに入力し、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;
}