The10thDoctor
The10thDoctor

Reputation: 125

C++ File accessing/ input and output

I'am having a rather difficult time with this program (see code below). It is supposed to :

  1. Create an array of 26 components to do the letter count for the 26 letters in the alphabet and a variable for the line count.

  2. Create an ASCII (or text) file that contains text and will be used as input to my program.

  3. Call that file "textinput" and then, have the output stored in a file called "textoutput".

Can anyone tell me what I'am doing wrong? I keep getting "File not found" errors.

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cstdlib>
#include <fstream>

using namespace std;

int main()
{
int lineCount = 0;
int letterCount[26];

for(int i = 0; i < 26; i++)
    letterCount[i] = 0;

ifstream infile;
infile.open("textinput.txt", ios::in);

if(!infile)
{
    cerr<<"File does not exist."<<endl;
    exit(1);
}

ofstream outfile;
outfile.open("textoutput.txt", ios::out|ios::binary);

if(!outfile)
{
    cerr<<"File cannot be opened."<<endl;
    exit(1);
}
char data[100];
outfile<<data;

while(infile>>data)
{
    outfile<<data<<endl;
}

while(infile)
{
    char ch1 = infile.get();
    if(ch1 == '\n')
    {
        lineCount++;
        continue;
    }

    int asciiNum = (int)ch1;
    if(asciiNum > 96)
    {
        asciiNum = asciiNum - 97;
    }
    else
    {
        asciiNum = asciiNum - 65;
    }

    letterCount[asciiNum]++;
}
infile.close();
outfile.close();
system("PAUSE");
return 0;
}

Upvotes: 1

Views: 3802

Answers (3)

0decimal0
0decimal0

Reputation: 3984

ifstream class is used to read from files and to read from files you must need to create it first which you haven't done, so first create the file .

By doing like this :

ifstream infile;

infile.open("textinput.txt", ios::in);

you are trying to read from a file which has not been created yet OR may be as described in other answer or the comments that your file doesn't exist in the same directory.

You better use ofstream to first write on the file and then use ifstream.

Upvotes: 1

sehe
sehe

Reputation: 393049

The funny thing is, "File not found" errors are not possible with your program.1 So, I'm going out on a limb and suggest that you need to qualify the path to your executable!

Say, you compiled with something like

gcc program1.cpp -o program1

To execute, you must use

./program1

Because program1 won't work. The reason is that with 99% certainty, your current working directory is not in the search PATH for executables (and you want to keep it that way).

Beyond this, yes, do make sure that the textinput.txt exists in the same directory.


1(There's no such error message in the program. You should know: you programmed it!)

Upvotes: 2

user2381184
user2381184

Reputation:

Does your code work if you have the file? If it does try removing the ios::out.If i'm not mistaken ios::out is used when you do not want to truncate your old content in the file,but that implies you already have it.

Upvotes: 0

Related Questions