English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have been able to solve the heap sort problem. The problem i have now is how to input from a file into a string. Its not as easy as it sound. it begins wiht a letter e.g. 'I' that means insert, 'S' for search, 'R' for remove or delete, 'C' for change and 'P' for print, all on multiple lines.
An example of an input will be:
I 2 34 56 78
P
S 2 30
R 56
P
I 45 67 43 23 45
C 45

2007-11-15 16:53:23 · 1 answers · asked by FrederickVonHausen 1 in Computers & Internet Programming & Design

i am not new to programming, but i am not too sure i know how to parse. I am a bit stuck, here is what i got so far there was more but i always get to blocked paths and i have start all over again.

if (myfile.is_open()){
fstream fileoperative("out2.txt",ios::out);
ifstream myfile("data.txt");
char buf[1024];
myfile.getline(&buf[0], 1024)
I tried using char and converting the first character into a char, but i am stuck with all the rest being chars. I am sure there is a way to do this. ANYONE OUT THERE!!!

2007-11-15 17:15:10 · update #1

the ifstream file is called myfile, and i am outputting to a file called out.txt.
okay i understand what you mean by i am using the getline wrong. But how on earth am i supposed to read in a letter first, then insert all the rest into an array. Here is an example of a previous solution
string line;
int charnum;
int i;
string d;
ifstream myfile ("data2.txt");
if (myfile.is_open()){
fstream fileoperative("out2.txt",ios::out);
while (! myfile.eof() ){
getline (myfile,line);
istringstream ss(line);
charnum=0;
bool flag=false;
int command=0;

int treeversion=0;
while( ss >> d ) {
if(charnum==0){
if(d[1]==49)
treeversion=1;
else if(d[1]==50)
treeversion=2;
else if(d[1]==68){
cout<<"Are theses damn Trees the same? ";
fileoperative< cout <<"They are not identical" << endl;

cout< fileoperative< }

2007-11-15 17:37:05 · update #2

1 answers

#include

read line by line
for each line, check the first char for the operation to perform, then parse the rest of the line and perform the operations... repeat until EOF

----UPDATES--------
if (myfile.is_open()){
fstream fileoperative("out2.txt",ios::out);
ifstream myfile("data.txt");
char buf[1024];
myfile.getline(&buf[0], 1024)

1. Why are you opening a file called out2.txt if you're trying to read an input file? Shouldn't it be called input2.txt or something? Also, for input you will want ios::in

You are using getline wrong. You are saying, take the first element of the buf buffer and store 1024 bytes into it. Just do
myfile.getline(buf, 1024)
Which will read 1024 bytes into buf. Then you can do buf[0] to get the first character

(ie. if ( buf[0] == 'P' || buf[0] == 'p' ) { ... }

------- MORE UPDATES--------
I think you are just confused about parsing.
See you only do one myfile.getline( buf, 1024 ) call
Get line will return after it has encountered a new line OR it has read 1024 characters (something to that effect it may only be 1023 but i dont recall off hand)

So now you have the entire line stored in buf
You first want to determine what operation you're going to be doing (as I described in the last update) and then you need to store the items you read into an array. Parsing is often done with the function strtok():

int values[ 50 ] ; // assume no more than 50 per line

int i = 0 ;

// offset by 2 to skip the command and space
char* textValue = strtok( buf+2, " " ) ;

while ( textValue != NULL ) {
values[ i++ ] = atoi( textValue ) ;
textValue = strtok( NULL, " " ) ;
}

// at this point your values should be stored in the array

// execute the command you read previously

// repeat the above for the remaining lines

2007-11-15 16:59:59 · answer #1 · answered by mdigitale 7 · 0 0

fedest.com, questions and answers