Forum Index | FAQ | New User | Login | Search

Make a New PostPrevious ThreadView All ThreadsNext ThreadShow in Flat Mode*


Subjectspace Reply to this message
Posted byTerry Bogard
Posted on06/16/04 01:26 PM



Back in the DOS days, the IDE I was using provided a cool options that converted tabs to spaces once you saved the file.

Also, in the same years I wrote a small and unoptimized program that converted tabs to a specified number of spaces in text files. Here it is. And yes, it's an exercise to learn how to use fstream classes.

//
// TABEXP.CPP
//

#include
#include

// self-explanatory
const unsigned short DEFAULT_TABSIZE = 4;

int main (int NArg, char *const arg[]) {

// help if invalid syntax is used
if (NArg < 3) {
cout << "\nTABEXP - Tab Expander"
<< "\nUsage:"
<< "\n"
<< "\n " << arg[0] << " infile outfile [tabsize]"
<< "\n"
<< "\nTab Expander expands all TABs in infile to spaces in outfile"
<< "\ninfile must be a text file, default tabsize is " << DEFAULT_TABSIZE
<< "\nWARNING: data loss MAY occur; if you lose data, I'm sorry :)"
<< "\n";
} else {
// correct syntax used

// open input file
ifstream inFile(arg[1]);

// error
if (!inFile.good()) {
cout << "\nSorry cannot open input file " << arg[1] << " - Exiting...\n";
return 1;
}

// open output file, cannot be overwritten (only at the moment...)
ofstream outFile(arg[2], ios::out|ios::noreplace);

// error
if (!outFile.good()) {
cout << "\nSorry cannot open output file " << arg[2] << " - Exiting...\n";
return 1;
}

// obvious
unsigned short tabSize = 0;

// tabSize is non-zero if third parameter exists and is valid
if (NArg > 3) tabSize = atoi (arg[3]);

// decide wether to use default value
if (tabSize == 0) tabSize = DEFAULT_TABSIZE;

// print some info on the console
cout << "\nUsing tabsize = " << tabSize;
cout << "\nWorking... ";

// ch is used for input and output; slow but it works well
char ch;

// a counter
int i;

// read char as long as it is not at EOF
// if it's a tab, then write 'tabSize' spaces in output
// if it's not, then copy in in output
while (!inFile.get(ch).eof()) {
if (ch == '\t') {
for (i=0; i < tabSize; i++) {
outFile.put(' ');
}
} else {
outFile.put(ch);
}
}

// politely close input file and *safely* close output file
inFile. close();
outFile.close();

// if you don't see this, start worrying...
cout << "operation completed.\n";

}

// return 'no-error' errorlevel
return 0;

}




-
Entire Thread
Subject  Posted byPosted On
*OT - Tab People and Space People  finaldave06/16/04 12:51 PM
.*Size 8 tabs, sucka. *stomp* n/t  Fortyseven02/23/06 09:04 PM
.*Tab person here  LordEvilElmo07/08/04 04:49 AM
..*Re: Tab person here  finaldave07/08/04 08:59 AM
.*Re: OT - Tab People and Space People  erikduijs07/07/04 09:28 PM
.*i'm spaceman spiff [nt] a space oddity  Joffeman07/04/04 10:43 PM
.*Re: OT - Tab People and Space People  R. Belmont06/27/04 02:05 PM
..*Re: OT - Tab People and Space People  finaldave06/29/04 06:52 AM
.*both  newsdee06/24/04 10:14 PM
.*Re: OT - Tab People and Space People  smf06/23/04 11:51 AM
..*never ever?  Terry Bogard06/24/04 09:19 AM
...*Re: never ever?  finaldave06/24/04 12:48 PM
....*Re: never ever?  Terry Bogard06/25/04 06:01 AM
.....*Re: never ever?  smf06/25/04 07:44 PM
..*Re: OT - Tab People and Space People  finaldave06/24/04 06:01 AM
.*tab person  Jonemaan06/21/04 08:31 AM
..*Re: tab person  erikduijs07/19/04 06:56 PM
..*Re: tab person  finaldave06/21/04 07:33 PM
...*I prefer tabs  Dracula-X06/23/04 04:08 PM
....*Darn - While we are on the topic  finaldave06/24/04 06:09 AM
.....*Re: Darn - While we are on the topic  MrJeff06/25/04 06:59 AM
.....*Re: Darn - While we are on the topic  Dracula-X06/24/04 06:36 PM
......*Re: Darn - While we are on the topic  smf06/25/04 07:47 PM
.......*Re: Darn - While we are on the topic  Videoman06/27/04 03:13 AM
........*Re: Darn - While we are on the topic  R. Belmont06/27/04 02:06 PM
.........*Re: Darn - While we are on the topic  smf06/28/04 10:11 AM
..........*Re: Darn - While we are on the topic  R. Belmont06/28/04 07:13 PM
...........*Re: Darn - While we are on the topic  smf06/30/04 03:23 AM
............*Re: Darn - While we are on the topic  erikduijs07/19/04 06:34 PM
............*Re: Darn - While we are on the topic  Videoman07/01/04 05:05 AM
.............*Re: Darn - While we are on the topic  smf07/01/04 08:05 AM
..............*Re: Darn - While we are on the topic  erikduijs07/19/04 06:21 PM
..............*Re: Darn - While we are on the topic  finaldave07/01/04 09:40 AM
...............*Re: Darn - While we are on the topic  R. Belmont07/02/04 10:52 AM
................*Re: Darn - While we are on the topic  erikduijs07/19/04 05:56 PM
................*Re: Darn - While we are on the topic  smf07/06/04 03:11 AM
.*Tabs  Tourniquet06/19/04 07:25 PM
.*4 spaces - NT - hating tabs  Pete B.06/19/04 05:12 PM
.*Re: OT - Tab People and Space People  Barry Rodewald06/19/04 08:07 AM
.*2 spaces! Anyone else? -nt-  finaldave06/19/04 05:11 AM
..*Re: 2 spaces! Anyone else? -nt-  Riff07/01/04 12:50 PM
..*Ewww -- 4 spaces or tabstops every 4 characters, please [nt]  Jan_Klaassen06/19/04 02:52 PM
...*so which was that?  finaldave06/20/04 05:18 PM
....*Re: so which was that?  Jan_Klaassen06/20/04 08:24 PM
..space  Terry Bogard06/16/04 01:26 PM
..*Re: space  Bart T.06/17/04 00:20 AM