![]() |
|
#1
|
||||
|
||||
Using files for inputMy latest problem involves using a file for input, rather than user input. My program below is based on my book, but it doesn't like the input file I tell it.
I'm currently coding it using Anjuta 1.2.2 in Libranet Linux 2.8.1. I don't know if I need to change the code because Linux has a different file structure than Windows; it shouldn't make a difference, right? I created a sample text file in my /home/cody directory; on execution, the program gave a Code:
If I make a copy of the file and put it in the directory where the program is, then the program just sits there after I type in the file name. If I tell it expressly where the text file is (e.g. /home/cody/Text_File), it also sits there. Do I need to modify my code for Linux or is it how I enter the file in the program when asked? CPP / C++ / C Code:
Thanks for everyone's help. :-) __________________
Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#2
|
||||
|
||||
|
Problem opening the file? Instead of...
CPP / C++ / C Code:
Try this... CPP / C++ / C Code:
__________________
-Aaron |
|
#3
|
||||
|
||||
|
No joy. I made the change and it still doesn't work. Exact same problems as before.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#4
|
|||
|
|||
|
Well, look at
CPP / C++ / C Code:
You have read a single character into your char variable line_char. If the first character on the first line is not '\n', it's an infinite loop. Also note CPP / C++ / C Code:
I'll bet it should be CPP / C++ / C Code:
(A real common typographical error, and sometimes hard to spot in the middle of lots of otherwise good code.) Good Luck! Dave |
|
#5
|
||||
|
||||
|
Crap. I didn't notice that :-(. I'll try that and see what happens. Thanks.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#6
|
||||
|
||||
|
Okay. Now it's reading the file (I think). At least it gives me an output. But there's obviously something wrong in my logic. Here's my new code:
CPP / C++ / C Code:
Here's my output: Code:
Thanks. I really appreciate the help; it's so much faster (and more informative) than waiting for my instructor to email me back. __________________
Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#7
|
|||
|
|||
|
Quote:
Here's something even faster and even more valuable to your productivity: put output statements at various places in your program to see what's happening. For example in the above comparisons, what are you actually comparing: What are the current values of short_line and long_line? What is the value of indiv_count? I could tell you, but why don't you try it yourself: CPP / C++ / C Code:
C++ programmers: cout<< is your friend. C programmers: printf() is your friend. Best regards from your other friend, Dave Last edited by davekw7x : 29-Aug-2004 at 14:16.
|
|
#8
|
||||
|
||||
Still confusedWell, I see what I suspected: after initializing short_line and long_line to the min/max values, they stay that way while in the loop. It also appears that trying to identify the newline doesn't seem to work; actually, it doesn't appear to loop at all.
I put in 3 cout>> groups: one prior to the eof check, one right after ++line_num, and one after the relationship if statements. It appears the final one isn't seen, as no output was given. My thought process is that after the relationship checks, the default values of "short_line" and "long_line" should be changed to whatever "indiv_count" is, but that doesn't appear to be happening. It's almost like the loop gets to the second "\n" if statement and stops. I'm getting very confused. But, thanks for the help. __________________
Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#9
|
|||
|
|||
|
Quote:
How about this: CPP / C++ / C Code:
I think you will find that cin>>line_char eats white space! (Including the newline) try CPP / C++ / C Code:
Now, seasoned C++ programmers probably use some other method, but I think you can get yours to work if you keep at it. There are lots of other errors that you can pick out after you get the program to do something. (For example you set indiv_count to 0 before you read each character!. You should set it to zero befor entering the loop, and set it to zero after you detect a '\n'.) There are lots of other hints that will jump out of the page after you get each line read and collect statistics. Good Luck, Dave |
|
#10
|
|||
|
|||
|
Quote:
I wanted to separate this from the other, more serious error (couldn't see the newline, so couldnt count lines). Now, look at what you do woth short_line and long_line. If you did the following before each comparison, you should see why short_line stays at its initial value: CPP / C++ / C Code:
What did you see: short_line = some verrrrry large negative number. Now, can your line length ever be less than that? I don't think so. So initialize short_line to some large positive number, then do your comparison. Similarly, you initialized long_line to some verrrrry large positive number. Now, can your line length ever be greater than that? I don't think so. Are you getting the picture? Regards, Dave |
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| controling dialog objects & multiple source files | omills | MS Visual C++ / MFC Forum | 0 | 14-Jul-2004 23:30 |
| Apache2 config issues | monev | Apache Web Server Forum | 2 | 28-Jun-2004 06:19 |
| this is from 6 files now i need fancy reports to write to the files | kilgortrout | C++ Forum | 1 | 21-May-2004 16:57 |
| Can't view pages from another machine on the Intranet | aevans | Apache Web Server Forum | 9 | 14-May-2004 02:26 |
| gxx linker accepts only 7 object files | danielxs66 | C++ Forum | 1 | 12-Dec-2003 09:27 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The