473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

input streams...? reading files...?

Okay, so I'm pretty new at this whole file reading thing, and I'm trying to make it so that the program reads a file, attaches a line number to the beginning of each line, and sends the output to another file. The thing is, there's this error that pops up, and I'm not exactly sure why.

The code that I have is as follows:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int main (void)
  5. {
  6.     char c;
  7.     int line = 0;    
  8.     ifstream IS = "hw6b.cpp";
  9.     ofstream OS = "hw6boutput.cpp";
  10.     while ((c = IS.get()) != EOF)
  11.         if ( c == '\n')
  12.         {
  13.             line++;
  14.             OS << line << '.' << '\t' << c;
  15.         };
  16.     return 0;
  17. }
  18.  
Is there something I'm doing wrong? Something I'm missing?
Nov 28 '06 #1
1 1356
sicarie
4,677 Expert Mod 4TB
Okay, so I'm pretty new at this whole file reading thing, and I'm trying to make it so that the program reads a file, attaches a line number to the beginning of each line, and sends the output to another file. The thing is, there's this error that pops up, and I'm not exactly sure why.

The code that I have is as follows:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int main (void)
  5. {
  6.     char c;
  7.     int line = 0;    
  8.     ifstream IS = "hw6b.cpp";
  9.     ofstream OS = "hw6boutput.cpp";
  10.     while ((c = IS.get()) != EOF)
  11.         if ( c == '\n')
  12.         {
  13.             line++;
  14.             OS << line << '.' << '\t' << c;
  15.         };
  16.     return 0;
  17. }
  18.  
Is there something I'm doing wrong? Something I'm missing?
There are three main things that I am seeing. The first is your declaration of the files. You need to use the stream.open(), for example:

Expand|Select|Wrap|Line Numbers
  1. ifstream IS;
  2. IS.open("hw6b.cpp");
  3.  
for both your input and output streams.

Your ';' after the if statement is not 100% necessary (though it certainly still works with it there), and although it's not a big deal in a small program like this, you should consider using the IS.close() operator as well (for both streams).

The final thing is that your algorithm is incorrect. Your program (once the syntax errors are corrected), will wipe out any of the lines, and just put the numbers in them. It's not a big thing, you just need to restructure a little bit. The first part is an else statement that will print the character if it is not a '\n', which was previously the only time it was printed... There are a few more small issues, but nothing major - you should be able to get it from there.
Nov 28 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Bob Smith | last post by:
question in subject. does the standard say anything about this or is it platform dependent? any advice much appeciated /B
21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
6
by: Nick R | last post by:
I need to know how to input bits from a *.wav file. I have tried this: #include <fstream> .... ifstream fin; fin.open("/root/foofile.wav", ios::in|ios::binary); and then inputting...
11
by: Kobu | last post by:
I have a question about C's abstract "streams" (that I can't seem to FULLY understand from reading several tutorials). Streams seems to suggest that input can be treated continously if needed....
9
by: Julien Biezemans | last post by:
Hi! Here is the problem: I'd like to restrict local filesystem stream operations to one directory just like a root jail. fopen('/file.bin') would actually open /some/path/file.bin. One goal...
0
by: satish | last post by:
Hi, I have an issue ..i have a text which has the information of 4 test files and they are placed in the order 4 --- this number says (total no of text files) File1 1370 -- Memory of the text...
77
by: arnuld | last post by:
1st I think of creating an array of pointers of size 100 as this is the maximum input I intend to take. I can create a fixed size array but in the end I want my array to expand at run-time to fit...
2
by: subramanian100in | last post by:
In K & R ANSI C book(2nd Edition), in page 241, the following lines are mentioned. "The library supports text streams and binary streams, although on some systems, notably UNIX, these are...
27
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.