-
I dont get it!
Ok, I was given an assignment for Computer Science AP to write a program to:
1. In function main, open two file streams:
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 begining if each line and replace them with an integer count of the number of blanks present. Use a setw(2) format-specifier to print the integer. Two spaces should follow after integer follow, then the rest of the line should be transferred unchanged.
here is what I have, but it only parses through once and comes up with bogus output because of it.
got any ideas?
help will be EXTREMELY appreciated
Code:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
int main(int argc, char* argv[])
{
if(argc>1)
abort();
char a;
int counter=1,lines=1, spaces[256], linenum=1;
ifstream in;
ofstream out;
in.open("c:\\luke\\new.txt");
out.open("c:\\luke\\new2.txt");
while(in.get(a))
{ if(a=='\n')
lines++;
}
while(in.get(a))
{
if(a==' ')
spaces[linenum]++;
else if(a == '\t')
spaces[linenum]+=4;
if(a=='\n')
linenum++;
}
out<<spaces[1]<<setw(2);
cout<<lines<<endl<<counter<<endl<<spaces[1]<<endl;
while(counter<=lines && in.get(a))
{
if(a=='\n')
{
counter++;
out<<endl<<spaces[counter]<<setw(2);
}
else if(a=='\t')
{
}
else
out<<a;
}
in.close();
out.close();
return 0;
}
-
How about this. Why don't you read a full line at a time and then parse through it? I think this would be much easier. You can read until a non-space character is reached and use that counter for the white space count. I think this would be easier.
-
yah I tried that, here is the problem
I parsed through each line but at the beginig is where I have to hace a space number like:
Code:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
if(argc>1)
abort();
char a;
int count=0,lines=0;
ifstream in;
ofstream out;
in.open("c:\\luke\\new.txt");
out.open("c:\\luke\\new2.txt");
while(in.get(a))
{ if(a=='\n')
lines++;
}
cout<<lines<<endl;
in.close();
out.close();
return 0;
}
will become
Code:
0 #include <iostream.h>
0 #include <fstream.h>
0 #include <stdlib.h>
0 int main(int argc, char* argv[])
0 {
3 if(argc>1)
6 abort();
3 char a;
3 int count=0,lines=0;
3 ifstream in;
3 ofstream out;
3 in.open("c:\\luke\\new.txt");
3 out.open("c:\\luke\\new2.txt");
3 while(in.get(a))
3 { if(a=='\n')
3 lines++;
3 }
3 cout<<lines<<endl;
3 in.close();
3 out.close();
3 return 0;
0 }
-
and the problem is???
You read in the line to a buffer....then you parse through detecting the number of spaces...then you write the correct output to a new file. I don't see where the problem is.
-
you know the funny thing is that I knew C++ before I went into this class, and he hasn't taught us arrays yet, I am curious how to do it otherwise.
-
ok that all work nice and dandy!
but now I have the WHOLE file in a buffer
and im going through the lines
how do I only take the spaces on the FIRST PART and leave the rest alone until I hit a newline.
The english sounds extrememly similar to the code but when I try it, it doesn't work...
I am stumped
-
Don't read the whole file into a buffer. Just read one line at a time.
-
k here is what I got for reading line=by=line but it doesn't work for the parsing:
Code:
while(in.getline(buff,1024,'\n'))
{
count=0;
i=0;
while(buff[i]=='\t')
{
count++;
}
out<<setw(2)<<count<<" ";
i=0;
while(buff[i]!='\0')
{ cout<<buff[i];
i++;
}
out<<endl;
}
-
I DID IT HURRAY!!!!!!!!!!!!!!!!
YAY
Thank you MrWizard
you have been a great help
Code:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <ctype.h>
int main(int argc, char* argv[])
{
if(argc>1)
abort();
char buff[20000];
int i=0,count=0;
ifstream in;
ofstream out;
in.open("c:\\luke\\new.txt");
out.open("c:\\luke\\new2.txt");
while(in.getline(buff,1024,'\n'))
{
count=0;
i=0;
do
{
if(buff[i]=='\t')
{
count+=3;
}
i++;
} while(! isgraph(buff[i]));
out<<setw(2)<<count<<" ";
i=0;
i=count/3;
while(buff[i]!='\0')
{
out<<buff[i];
i++;
}
out<<endl;
}
return 0;
}
-
Code:
while(buff[i]!='\0')
{
out<<buff[i];
i++;
}
Has the same effect as:
B.t.w. Tab isn't the only blank, what about a space?