-
File Processing
I have a .dat file that has about 5000 words in it. I would like a program that removes all string that are more than 5 letters long and that has an upper-case character. For example, if this was my original file:
Code:
Jim
jumpy
cooks
Brite
spark
my new file would only contain the following:
I am asking this since I am a complete newbie at file processing and I need someone to point me in the right direction. Thank you.
-
Get some basic code up and we'll help you from there :D .
-
Literally, all I know are the basics. That is, I know how to read from a file but thats it. Here goes:
Code:
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
int main()
{
ifstream inClientFile("data.dat", ios::in);
if(!inClientFile){
cerr<< "File cannot be opened";
exit(1);
}
}
That's basically it. See what I mean? I do not know how to read for specific characters, length of characters, etc. Please help!
-
ifstream works much like istream:
Code:
ifstream fin;
fin.open("somefile.txt");
fin >> aVar >> anotherVar;
getline(fin, var3);
-
Let's start off with some slightly modified basics. Here's a little different version of your code:
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main()
{
ifstream inClientFile("data.dat", ios_base::in);
if(!inClientFile.is_open()){
cerr<< "File cannot be opened";
return 1;
}
// do file stuff here
return 0;
}
First you need somewhere to save read data. (ie: char text[100];)
You also need a temporary file.
Then you need to read each line and write it to the temporary file if it doesn't have a capital or if its length is <= 5.
Then you need to close both files and rename the temp file to the original file.
[edit]
To offer a little more, do something like this:
Code:
while (inClientFile.getline(text, 100).good()) {
if (!hasCapital(text) && strlen(text) <= 5)
tempFile << text << endl;
//...
}
// then close both files
// then delete the source file
// then rename the temp file to the source file name
That should get you on your way...
-
An even easier way to read strings:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inClientFile("data.dat", ios_base::in);
if(!inClientFile.is_open()){
cerr<< "File cannot be opened";
return 1;
}
string line;
while(getline(inClientFile,line)){
//Check line for correctness here
}
}