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

Recommended Answers

All 2 Replies

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.

you can use

strtok (char*,char);

something like this:

char* str="string literal";
char* string=strtok(str," ");
char* literal=strtok(NULL," ");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.