GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Apr-2005, 22:15
demon777 demon777 is offline
New Member
 
Join Date: Apr 2005
Posts: 2
demon777 is on a distinguished road

Reading a file C++ hElp!!:(


Hi guys,
I'm trying to read in a file, it's contents are in the form:
17.00 Mount:Teressa Adventure
18.00 Discover
...
Now I need to store 17.00,18.00 etc in a 2d array(upper row), and MMount:Teressa Adventure(the whole string) in the same 2d array, so as to display it in a tabular form later on.
I'm close to tearing my hair apart on this..any help would be greatly appreciated.
  #2  
Old 03-Apr-2005, 00:48
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by demon777
Hi guys,
I'm trying to read in a file, it's contents are in the form:
17.00 Mount:Teressa Adventure
18.00 Discover
...
Now I need to store 17.00,18.00 etc in a 2d array(upper row), and MMount:Teressa Adventure(the whole string) in the same 2d array, so as to display it in a tabular form later on.
I'm close to tearing my hair apart on this..any help would be greatly appreciated.
Welcome to GID demon.

Unfortunately, we can't help you yet because we have no idea what you know nor what you've tried. You need to help us by explaining everything you can about the problem, and post the section of the code you're having trouble with so we know what to tell you. Be sure to read the sticky at the top of the forum.

Rule of thumb:
1) post some code
2) explain what it's supposed to do
3) explain what it actually does
4) tell us where and what you think the problem is
5) if compiler errors paste the errors here (do NOT paraphrase them)
6) Make sure we can find the lines by placing comments in the code -- many of us won't count to line 130 when a comment could show us where 130 is immediately

Try to keep code segments short, under 20 lines if possible.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #3  
Old 03-Apr-2005, 04:50
demon777 demon777 is offline
New Member
 
Join Date: Apr 2005
Posts: 2
demon777 is on a distinguished road
Quote:
Originally Posted by WaltP
Welcome to GID demon.
Thanks,
Sorry I should have been more clearer/read the stickies before posting out
The text file is in this format:
8
=ABC
18.00 Dr Who: Underworld
18.30 Collectors
19.00 News
19.30 The 7.30 Report
20.00 Catalyst
=Seven
18.00 Seven News
18.30 Today Tonight
19.00 Home and Away
19.30 My Restaurant Rules
20.30 **END
....
And the problem asks to print this out in a tabular format/ as a tv guide, with the timings at the top, and channels on the left. This is the partial code I wrote:
CPP / C++ / C Code:
string table[4][6];
ifstream inFile;
inFile.open("guide.txt");
getline(inFile,width);
n=atoi(width.c_str());//Cell width for the table
    
while(!inFile.eof())
	{
             getline(inFile,table[i][0]);
	while(strcmp(&ch[0],"=")!=0)
	       {
                    inFile>>table[0][j]>>skipws>>table[l][k];//problematic part
            	       inFile.getline(ch,100);
	       j++;
	       k++;
	       }
             l++;
             i++;
			
            }
This further goes on to print the table and format the output etc...the part I can't figure out is...how can I parse the text and put the time and the whole program name string in the 2d array, right now when I test this, table[1][1] for eg has just Dr instead of the whole Dr Who: Underworld. I'm sure it's something stupid i'm missing
Cheers guys,
  #4  
Old 03-Apr-2005, 22:44
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Well, Here's what I see right up front:

CPP / C++ / C Code:
while(!inFile.eof())
See this information, the post on feof (#2)


CPP / C++ / C Code:
getline(inFile,table[i][0]);
Read the line into a temporary buffer, not your matrix. You don't know if it belongs in your matrix yet. Let's call the buffer inputbuf[] for the moment.


CPP / C++ / C Code:
while(strcmp(&ch[0],"=")!=0)
ch seems to be undefined.
Since ch is a char, simply used
CPP / C++ / C Code:
while(ch[0] == "=")
It's faster cleaner, and works. strcmp in this case won't work at all.
strcmp compares strings and the string "=" is never equal to "=ABC"


The rest of the posted code only executes as long as you read in a line starting with "=".
Consider changing the second while to an if:
CPP / C++ / C Code:
if (inputbuf[0] == '=')
{
    // process the = line
}
else
{
    // here you process the time/show line.
}


This is processing the time/show line...
CPP / C++ / C Code:
inFile>>table[0][j]>>skipws>>table[l][k];//problematic part
You have the input line now in inputbuf. Is the first SPACE in the line always between the time and show? If so, search for the space. Then you can move everything before the space into the time portion or your array entry, everything after into the show portion.

You take it from there.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:33.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.