LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-31-2008, 01:11 PM   #1
assamite
LQ Newbie
 
Registered: May 2008
Posts: 1

Rep: Reputation: 0
Question 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
 
Old 05-31-2008, 01:23 PM   #2
LordAnta
Member
 
Registered: Apr 2008
Location: Munich, Germany
Distribution: Slackware current
Posts: 128

Rep: Reputation: 18
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.

Last edited by LordAnta; 05-31-2008 at 01:27 PM.
 
Old 05-31-2008, 04:54 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
USB stopped working -- lsusb results in: "Unknown line at line" jgombos Linux - Hardware 3 04-20-2008 06:37 PM
file format why difference between "command line and GUI interface" hocheetiong Linux - Newbie 3 09-17-2007 07:19 AM
Samba "badly formed line in configuration file" maerong Linux - Networking 5 02-02-2005 05:19 PM
What does this mean? suSE9.1 ATI "/bin/sh: line 1: scripts/modpost: No such file or" op_stager Linux - Hardware 0 09-29-2004 11:28 PM
Can I redirect script output to a file without ">> $LOGFILE" at the end of each line davee Linux - General 1 12-19-2003 05:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration