Hello new to the forum, I've been learning and working with C++ for a few months now and I've started to learn how to use strings. What I'm trying to do is take each individual line from a text file and dissect all blocks of text separate by a space and send those into a two dimensional array. So in other words, I want the first line of text to be in row[0] of an array, while all of the words hold places in the columns. I want to repeat that method for the proceeding lines, except each new line will get a new row in the matrix and its columns will hold the text from that line.

This is the code I have for reading a text file, and I've tried to tokenize the words, however I have come into many syntax errors and I'm hoping someone can show me the correct syntax.

// read back text from a text file 
#include <iostream>
#include <fstream>
using namespace std;

void main()
    char temp_str[81];

    ifstream from_file;
    if (!from_file)
        cout << "Cannot open file - mytext.txt";
        exit(1);  //return to the OS with exit code 1

    cout << "This is the text in the file:\n";
    from_file.getline(temp_str, 80);
    while ( !from_file.eof() )
        cout << temp_str << "\n";
        from_file.getline(temp_str, 80);


mytext.txt holds

3 lines 3 words 15 characters
4 lines 9 words 39 characters

For starters, your main() is declared with a void return. It should ALWAYS be declared with an int return.

Beyond that, we can't really help you without specific error information.

Edited 6 Years Ago by Fbody: n/a

you can use

strtok (char*,char);

something like this:

char* str="string literal";
char* string=strtok(str," ");
char* literal=strtok(NULL," ");
This article has been dead for over six months. Start a new discussion instead.