# file streams...



## Mac Osxtopus (Dec 18, 2002)

Im sure all you bordlanders have seen this program, but i cant get it to dang work. Have a look: (no syntax errors reported, it compiles fine , but the output file is empty. including header files is fine, tthe forum just doesnt like alligators.)



#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>

main()
{
 ifstream infile("build/before.txt");
 ofstream outfile("after.txt");
 int counter;
char letter;

 infile.open("before.txt",ios::in);  // the yawn is a : with an o
 if(infile.fail())
 {
  cerr << "the file didn't open correctly";
  abort();
  }

 while(infile >> counter && infile.get(letter)){
 outfile.open("after.txt",ios:ut);
 outfile << setw(2) << counter << letter;
 }
 infile.close();
 outfile.close();
 return 0;
 }


----------



## anarchie (Dec 19, 2002)

If you enclose your code in vB code tags and disable smilies for your post, your code will be much more legible.

In this line, you create an ofstream object and open it to the file "after.txt" with the default mode ios:ut.

```
ofstream outfile("after.txt");
```

A side effect of opening the file with ios:ut is that its contents are destroyed.  "Clobbered" is either slang or jargon for this occurence.  I forget which.  

In this loop, the file gets reopened, and its contents deleted each time through:


```
while(infile >> counter && infile.get(letter)){
outfile.open("after.txt",ios::out);
outfile << setw(2) << counter << letter;
}
```

Simply remove the outfile.open() statement in the while loop to avoid the file-clobbering behavior.


----------



## Mac Osxtopus (Dec 19, 2002)

i did remove it... it still doesnt work  the after.txt file is completely empty. What the program is supposed to do is remove whitepsace characters at the beginning of lines and replace the first whitespace character with an integer value. Heres an example:

//Before whats its supposed to do..(before.txt)

Multiple forms of
   Indentation	
     muahahah  

//after what its supposed to write to the after.txt file.
0
0Multiple forms of
1Indentation	
5muahahah
0


----------



## anarchie (Dec 19, 2002)

Since you didn't put your example in code or preformat tags, your indentation was removed.  I can only assume that you meant to replace an initial run of spaces with a number equal to the number of spaces removed.  What you would have to do is read an individual character with infile.get() and compare it to a space character(' ').  If it is a space then increment a counter and read another character, if not, output the counter, read the remainder of the line with infile, and output that remainder.


----------



## Mac Osxtopus (Dec 19, 2002)

yarrgh i just cant seem to figure otu whats wrong, it'll either give an error that it cant find the file before.txt or it just makes an  empty after.txt file!! 
//due to lots of hw, i didn't bother doing any of the tag stuff to make the alligators be ignored. If you'd like the assignment directly, have a look on the exact instructions:
Assignment:
in function main, open two file stream:
a. an ifstream linked to the original text file, the before version.
b. an ofstream linked to a new file name, the after version.

2. the program will remove the blanks at the beginning of each line and replace them with an integer count of the number of blanks present. Use a setw(2) format-secifier to print the integer. Two spaces should follow after the integer value, then the rest of the line should be transferred unchanged.

And now, here is my horribly attempt at doing it   

#include <fstream.h>
#include <iomanip.h>

main()
{
 ifstream infile("build/before.txt");
 ofstream outfile("after.txt");
 int counter;
char letter;

 infile.open("before.txt",ios::in);
 if(infile.fail())// incase if the file before.txt isn't found, give an error
 {
  cerr << "the file didn't open correctly";
  abort();
  }
   while(infile >> counter && infile.get(letter)){// for some reason, it wont let ///me cut off  infile >> counter, i always get a "couldn't find file" error message. 
 if(-1==counter) {
 counter=0;
 }

 if(letter=' ' && counter!=-1) {
 counter++;
 }
 // this is to count the amount of whitespace before an actual non-whitespace char
 else {// if all else works, write the part of the file.
 outfile.setw(2) << counter << letter; 
 //outfile << counter; 
 //outfile << letter;
 counter=-1;
 }
 outfile << setw(2) << counter << letter;
 }
 infile.close();
 outfile.close();
 return 0;
 }


----------



## anarchie (Dec 19, 2002)

You do not need infile>>counter.  This statement reads a formatted integer from infile.  What you are trying to read is spaces, not formatted integers.  Check the location of your executable and your files relative to it.  Is there really a "build" folder in the same folder as the program?  Is there really a before.txt inside that "build" folder?

What you appear to be doing now is counting ALL the whitespace in a line and converting runs of whitespace to numbers.  What you need to do if a character is NOT a space is output the count of blanks and the two-space padding, reset the counter to 0, output that non-space character, then read the entire line with infile.getline and output that.

And it really isn't hard at all to make your code look nice.  Just do this:

```
[code]
//code goes here
[/code ] //without that space
```


----------



## Mac Osxtopus (Dec 20, 2002)

YES, all the file paths SHOULD be correct... but for some reason it gives an error message, it has to do with something else...
...unfortunately, i'm going to fail this class, damn mr. donaker          because this is just ridiculous, ive literally spent 3/4 OF THE DAY WORKING ON THIS GODDAMN THING. AND I STILL HAVEN't GOTTEN IT RIGHT. I'd understand if it was something huge and complicated but JESUS CHRIST MAN, ITS SO SMALL.


----------



## anarchie (Dec 20, 2002)

Umm...are you using Codewarrior or Apple's dev tools?  With Codewarrior and Carbon iostreams, I believe the directory separator had to be a :, as in "build:before.txt".  Also, what is the exact text of the error message you're getting?


----------



## Mac Osxtopus (Dec 20, 2002)

It's the error in the program if the file isn't found, you know, the "error, the file couldn't be found" part.
I use apple's dev tool, Project Builder.


----------



## anarchie (Dec 20, 2002)

I looked at that code again, and it seems you're still opening infile multiple times.  And with two different files, no less.  This line creates AND opens infile on the file build/before.txt:

```
ifstream infile("build/before.txt");
```
This line, which followed it, opens infile a second time on a different file.  This is bad because it confuses both you and the program.

```
infile.open("before.txt",ios::in);
```
Remove the second one, and make sure that the first one contains a path to an actual file.  When you create infile and pass "build/before.txt" to its constructor as in the first line, you need NOT call infile.open() to open it.  If you want to open a different file with the same ifstream object, you must call infile.close() to close it before using open.


----------

