Iterating through each line in a file in C++

Viewed 49

I want to iterate through each line of a file in C++ and see if each line has a valid run, print, copy, and delete command along with some other things. This is the code I have but I keep getting a bunch of errors. I think my main problem is that I am iterating through each line incorrectly. Can someone tell me what I am doing wrong and correct it?

#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <fstream>
using namespace std;

int main()
{
    string filename;
    cout << "\nEnter filename: ";
    getline(cin, filename);

    ifstream myfile(filename);

    if (!file) {
        cout << "\nFile cannot be opened " << filename << endl;
        return EXIT_FAILURE;
    }
    string line;
    int total=0, commented=0, valid=0, invalid=0, run_lines=0, print_lines=0, copy_lines=0, delete_lines=0, len=0;
    int i;
    while(! filename.eof()){
        getline(filename, line)) {
        total++;
        len=line.size();
        for(i=0;i<=len;i++){
            if(line[i]=='%'){
                commented++;
            }
            if(line[i,3]=='run'){
                run_lines++;
            }
            if(line[i,5]=='print'){
                print_lines++;
            }
            if(line[i,4]=='copy'){
                copy_lines++;
            }
            if(line[i,6]=='delete'){
                delete_lines++;
            }
        }
        valid = commented + run_lines + print_lines + copy_lines + delete_lines
    }
    }
    file.close();
    cout << "Total lines: " << total << endl;
    cout << "Commented lines: " << commented << endl;
    cout << "Valid Command lines: " << valid << endl;
    cout << "Invalid Command lines: " << invalid << endl;
    cout << "Run commands: " << run_lines << endl;
    cout << "Print commands: " << print_lines << endl;
    cout << "Copy commands: " << copy_lines << endl;
    cout << "Delete commands: " << delete_lines << endl;
0 Answers
Related