1

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;
}
4
  • 1
    It seems that textinput.txt is not in the same directory as your executable... Commented Jul 13, 2013 at 18:03
  • I agree with Pierre Fourgeaud. Commented Jul 13, 2013 at 18:04
  • You have to create textinput.txt (with some text) using any editor and save it in directory with your program. Commented Jul 13, 2013 at 18:11
  • 1
    I think it's the case of reading the messages really carefully. See my answer :) Commented Jul 13, 2013 at 18:21

3 Answers 3

2

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!)

Sign up to request clarification or add additional context in comments.

2 Comments

My mistake, I meant to say "File cannot be opened".
@The10thDoctor Either you haven't created that file or as sehe says you haven't made sure that the file exist in the same directory.
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.