LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ input file reading "line by line" (https://www.linuxquestions.org/questions/programming-9/c-input-file-reading-line-by-line-646089/)

assamite 05-31-2008 01:11 PM

C++ input file reading "line by line"
 
Hello,
i ve a technical problem about my homework. I m gonna make dfa(deterministic finite automata) but my problem is about reading the file that giving the machine's properties.

my text file is like that:

4 6
0 0 1 a –1
1 0 3 a 2 b –1
2 1 1 b –1
3 0 0 a 3 b –1
-1


-1' s means 0. state is finished, 1. state is finished ..etc. except the first one, each line is about a specific state. i need to put them in a array(my idea) separately. so for example i need an 4 different array for this example. how could i tell the array "when you see -1 finish the array and make a new one" (something like that(: )
actually i just need to know that when the input pass the other line..
ps.any other idea's would be appreciated. =)

formal description:
"The first integer you will read will be the number of states in the DFA, while the second integer you will read will be the number of transitions. Then, you will read a sequence of blocks of the sort

StateNumber Final NextState1 Symbol1 NextState2 Symbol2 … NextStateM SymbolM -1


-1

terminated with a –1, where StateNumber is an integer which is the label of a state,Final is 0 if this state is not a final state and 1 if it is a final state. Next, the NextStatei Symbol pairs describe the transitions: For instance, from this state you can go to state labeled NextState1 with symbol Symbol1. NextState1 is an integer while Symbol1 is a single character. You can assume that all symbols will from the ASCII character set (that is one or A-Z, a-z, 1-9, and possibly punctuation – but no special characters such as ‘ğ’ or ‘â’) As you read, when you encounter a NextState which is –1, this indicates the end of the transitions for the state StateNumber. Note that each state may have different number of outgoing transitions."

and my code for read:
//all into one array in here..

ifstream input;
input.open("dfa.txt");

if(!input)
{
cout << "Error: file could not be opened" << endl;
exit(1);
}

int size=0;
char *dfaArray;

dfaArray = new char[size];


for ( size = 0; input>> dfaArray[size];)
size++;

input.close();
cout<<dfaArray; // for check the reading

LordAnta 05-31-2008 01:23 PM

In your code, you will get a big Segmentation Fault.
Try this way:

Code:

#include <string.h>
#include <ctype.h>

char array[300]; // or whatever value
int size = 0;
char c;

while (!input.eof())
 {
  input >> c;
  if (c!='\n' && !isspace(c)) // let's skip the newline character and spaces
    {
    array[size] = c;
    size++;
    }
 }

Next time use [_CODE] Your source code here [/_CODE] (without the _). It a lot easier to read.

tuxdev 05-31-2008 04:54 PM

You can use std::getline to read into a string in a completely mem-safe manner (rather than the error-prone raw C-style), but the spec really invites using >> and a state machine to parse it.


All times are GMT -5. The time now is 07:15 AM.