Thread: File Processing

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    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:
    Code:
    jumpy
    cooks
    spark
    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.

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Get some basic code up and we'll help you from there .
    Do not make direct eye contact with me.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    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!

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    ifstream works much like istream:
    Code:
    ifstream fin;
    fin.open("somefile.txt");
    fin >> aVar >> anotherVar;
    getline(fin, var3);
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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...
    Last edited by LuckY; 11-10-2003 at 05:55 PM.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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
      }
    }
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM