Reading file

This is a discussion on Reading file within the C++ Programming forums, part of the General Programming Boards category; I have a fairly simple assignment where I have to read a file, and print how many times each character ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    26

    Reading file

    I have a fairly simple assignment where I have to read a file, and print how many times each character occurs in an output file - I know how to do that, and I have that part done.

    What I need to know is how to count how many times it reaches the end of a line, and moves to the next one, stopping this when it reaches the end of file.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    When you read from data from a file, read through the arrays on a char by char basis, using switch / case statements to increment a counter for each letter depending on which letter the switch statement evaluated to. I would assume you've done something along these same lines already. Well then to add this functionality to your program, you really don't have to go that far. \n is an escape character that means a new line. So...

    Code:
    switch(input)
        case 'A': A++
        ...
        case '\n': lines++;

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    ... or just declare an array with the maximum number of chars in your encoding set. then index the array with the chars. As Mr. Mackrory mentioned, newline is just another character. i think its ASCII value is 10 or something...
    Code:
    int count[256];
    char ch;
    memset(&count, 0, 256 * sizeof(int));
    
    //read file
    in >> ch;
    count[ch]++;

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    26
    Quote Originally Posted by Perspective
    ... or just declare an array with the maximum number of chars in your encoding set. then index the array with the chars. As Mr. Mackrory mentioned, newline is just another character. i think its ASCII value is 10 or something...
    Code:
    int count[256];
    char ch;
    memset(&count, 0, 256 * sizeof(int));
    
    //read file
    in >> ch;
    count[ch]++;

    Thanks you two for help. Having three months off in the summer without doing much coding fried my brain . I was getting hung up on the fact that the professor mentioned that lines could have any number of characters up to 80, one may have 10 and another may have 79. Its amazing at the end of last semester I had a algorithm that had 6 for loops nested along with lots of if's using stuff from different classes (dirty I know, but it was the quickest method I could find ), and I had no problem looking at it and understanding it, yet this simple review stuff is throwing me off. I guess thats why its review.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Well I should point out that there are issues on different OSs when dealing with stuff like the new line character. Different implementations deal with the concept very differently, but for a class / review problem, I think you'll be fine. Just something to consider in future.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    26
    ARGH. Stuck on one more thing. I'm getting hung up on the part where it opens the input and output files. Heres the code I'm using to open them:

    Code:
    fin.open(ifile_name);
    Code:
    fout.open(ofile_name)
    Thats great except it gives me an error I can't use a const there. So how do I ask a user for the name to get? Make it a pointer?

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    char ofile_name[80];

    cout << "enter file name and extension, e.g. text.txt" << endl;
    cin >> ofile_name;

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    i dont understand what your saying...

    post the code that sets the file variables and the actual compiler error.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    26
    Eh heres my entire code. I fixed that bug, except now I'm just plain getting frickin WEIRD stuff that I've no clue how to fix.....

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    // *** main *** ///////////////////////////////////////////////////////////////
    int main (void)
    {
      ifstream fin; // Input file stream
      ofstream fout; // Output file stream
      bool continue_program = false; // Boolean to decide to continue or not
      char ifile_name [90]; // Holds file name
      
      bool open_input (ifstream& fin, char ifile_name[]);
      bool open_output (ofstream& fout, char ifile_name[]);
      void read_file (ifstream& fin, ofstream& fout);
      
      // Loop calls open_input as long as continue_program is false
      // Program continues after continue_program is true
      while (!continue_program)
       {
        continue_program = open_input(fin, ifile_name);
       } // while
      
      continue_program = false;
    
      // Loop calls open_output as long as continue_program is false
      // Program continues after continue_program is true
      while (!continue_program)
       {
        continue_program = open_output(fout, ifile_name);
       } // while
    
      read_file (fin, fout);
    
    } // main
    
    
    // *** open_input *** /////////////////////////////////////////////////////////
    // Function opens the input file
    bool open_input (ifstream& fin, char ifile_name[])
    {
      bool file_error = false;
      
      cout << "Which input file do you want to open? ";
      cin >> ifile_name;
      fin.open(ifile_name);
    
      // If file open fails, file_error set to true
      if (fin.fail())
       {
        cout << "Unable to open input file" << ifile_name << endl;
        file_error = true;
       } // if
    
      return file_error;
    } // open_input
    
    
    // *** open_output *** ////////////////////////////////////////////////////////
    // Opens output file
    bool open_output (ofstream& fout, char ifile_name[])
    {
      bool file_error = false;
      char ofile[90];
      
      cout << "Which output file do you want to open? ";
      cin >> ofile;
      
      // If output fails, file_error set to true
      if (ofile == ifile_name)
       {
        cout << "Unable to open output file. Output file contains same name as "
             << "input.";
        file_error = true;
       } // if
       
      return file_error;
    } // open_putput
    
    
    // *** read_file *** //////////////////////////////////////////////////////////
    // Reads input file, then calls print_output
    void read_file (ifstream& fin, ofstream& fout)
    {
     char character; // Holds a single character temporarily
     char characters_encountered[80]; // Array to hold characters encountered
     int character_count = 0; // Counts how many characters have been encountered
     int null_count = 0; // Counts null characters
     int character_encountered_counter[80]; // Array to hold how many characters of
                                            // a type were encountered
     
     void search_characters(int character_count, int null_count,
                            char character, char characters_encountered[],
                            int character_encountered_counter[]);
     
     void print_output(int character_count, int null_count,
                       char characters_encountered[],
                       int character_encountered_counter[]);
     
     // Loops continues until end of file
     // Arrays are (hopefully) filled after loop
     while (fin >> character)
      {
       ++character_count; // Increase amount of characters read so far
       search_characters(character_count, null_count, character,
                         characters_encountered, character_encountered_counter);
      }
    
      print_output (character_count, null_count, characters_encountered,
                    character_encountered_counter);
    } // read_file
    
    
    // *** search_characters *** //////////////////////////////////////////////////
    // Searches for if character read has already been read before
    void search_characters(int character_count, int null_count, char character,
                           char characters_encountered[],
                           int character_encountered_counter[])
    {
     int i = 0;
     
     // Loop searches while i is less than characters counted so far
     // The character found is either found to exist already, or is added
     for (i = 0; i < character_count; i++)
         {
      //    if (characters_encountered[i] == "\0")
      //     {
       //     ++null_count;
      //     } // if
          if (characters_encountered[i] == character)
           {
            character_encountered_counter[i] += 1;
           } // else if
          else
           {
            characters_encountered[i+1] = character;
            character_encountered_counter[i+1] = 1;
           } // else
          } // for
    } // search_characters
    
    // *** print_output *** ///////////////////////////////////////////////////////
    // Prints arrays to output file
    void (int character_count, int null_count, char characters_encountered[],
          int character_encountered_counter[])
    {
     int i = 0;
     
     fout << "NEW LINE OCCURED " << null_count << "TIMES" << endl;
     // Loop writes data in arrays
     // Data in arrays is written after completion
     for (i = 0; i < character_count; i++)
      {
      fout << characters_encountered[i] << "OCCURED "
           << characters_encountered_counter[i] << setw(4) << "TIMES" << endl;
      } // for
    } // print_output
    *Edit*

    Maybe I should post the errors...

    152: parse error before 'int' (152 is the function header for print_output)

    162: syntax error before '<<' token (fout statement before for loop in print_output function)

    165: character_count was not declared

    165: parse error before ';' token

    165: syntax error before '++' token
    Last edited by OttoDestruct; 08-26-2004 at 05:03 PM.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    was this

    Code:
    void (int character_count, int null_count, char characters_encountered[],
          int character_encountered_counter[])
    supposed to be this:

    Code:
    void print_output(int character_count, int null_count, char characters_encountered[],
          int character_encountered_counter[])
    I think that should fix
    152: parse error before 'int' (152 is the function header for print_output)

    I don't see anything particularly weird about this error: it says it doesn't like something about the indicated line.




    Dave
    Last edited by Dave Evans; 08-26-2004 at 06:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21