Mam problem z kodem. Chcę by program wczytywał mi z pliku txt np.:
p,q#s
p#s,r
p#r
I przy pomocy funkcji strtok rozdzielał mi dane względem znaczka # tak by na końcu wyszło coś takiego:
p,q
s
p
s,r
p
r
Bardzo proszę o pomoc. Oto kod:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdafx.h>
using namespace std;
int main () {
const int maxCharsPerLine = 100;
const int maxLineInFile = 10;
char line[maxCharsPerLine];
char arrayOfChars[maxLineInFile][maxCharsPerLine];
int i=0;
char *Words[50];
char *pch;
sting myFile;
ifstream myFile ("text.txt"); // Otwarcie pliku txt
if (myFile.is_open())
{
while (! myFile.eof() )
{
myFile.getline (line,100,'\n'); //Ograniczenie długości lini
strcpy(arrayOfChars[i], line);
i++;
cout << line << endl;
pch = strtok (line,"#");
while (pch != NULL)
{
pch = strtok (NULL, "#");
if(strlen(pch)==4)
{
cout<<pch<<'\n';
int i=0;
strcpy(Words[i],pch); // dodaje słowa do tablicy
i++;
}
pch = strtok (NULL, "#");
}
}
myFile.close();
}
else cout <<"Nie mozna otworzyc pliku"<<endl;
system("PAUSE");
}