Jump to content
  • Advertisement
Sign in to follow this  
Boooke

fstream object count letters - and filename

This topic is 2472 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey. I am reading through "C++ Primer Plus", 5th Ed. Chapter six has this exercise (Number eight), which requires to count the letters in a file. Though, I haven't figured why my "counting" variable won't increment for each letter. Also, is there a way to write the input file's name? I was trying to play around with a string, but no luck, and couldn't find much about this on the web. Anyway, here is the code:


#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc, char * argv[])
{
if(argc < 2)
{
cout << "Drag file onto .exe";
}
ifstream inFile;

for (int i = 2; i <= argc; i++)
{
inFile.open(argv);
int count = 0;
while(inFile.good())
count++; // Not really counting anything.
cout << "There is " << count << " letters in <file>.\n"; // Exchange "<file>" with real filename.
inFile.close();
}
cin.get();
return 0;
}


I am not even sure if this really opens the file(s) dragged onto the .exe, since I haven't tried this method before?

Regards,
Boooke

Share this post


Link to post
Share on other sites
Advertisement

Also, is there a way to write the input file's name?

What exactly do you mean? Write the filename out to the console?



while(inFile.good())
count++; // Not really counting anything.


This says "While the file is good and can be read, increase count." The problem is that you aren't doing anything to the file, so if it's good at first, it'll stay good forever (since you are doing nothing to change its state). You need to read from the file, at least. Throwing a inFile.ignore() in the while loop would tell the file stream to ignore one character and advance itself, which should solve that problem.

Share this post


Link to post
Share on other sites
Hey, still no luck with the inFile.ignore(). I tried making a char, and redid the whileloop as

while (inFile.get(ch))

But it still says "0 letters in the <file>" which I also forgot to mention in first post about the first while-loop.


What exactly do you mean? Write the filename out to the console?

Exactly. If the filename was text.txt, it would write "...text.txt".

Share this post


Link to post
Share on other sites

But it still says "0 letters in the <file>" which I also forgot to mention in first post about the first while-loop.

Well if it's saying that, then my guess is that the file isn't good to begin with, which usually means that the file couldn't be found. See below about checking the file name and path and making sure it's correct. On Windows, it could also be that you have the file open in another program, which is blocking your program from opening it.


[quote name='Cornstalks' timestamp='1331143115' post='4920134']
What exactly do you mean? Write the filename out to the console?

Exactly. If the filename was text.txt, it would write "...text.txt".
[/quote]
In that case:

cout << "Attempting to open file " << argv << "\n"; // argv should be the filename, as that's what you're passing to inFile.open()

Double check that it's correct, as I'm guessing you're passing the wrong file name/path.

Also, your for loop starts at 2... I haven't ever made a program where you drag and drop stuff onto it, but shouldn't it start at 1? I'm assuming of course that you want the first argument in the command line to be interpreted as a file name to work with.

argv[0] should contain the program's file name.
argv[1] should contain the first argument passed to the program
argv[2] should contain the second argument
argv[3] should contain the third argument (and so on)

Share this post


Link to post
Share on other sites
D'oh. That was the solution. I've never been too sure on that. Before, I concluded the program always had two arguments, but now I see it does not. Thanks!

Btw, using inFile.good() with inFile.ignore() seems to count the EOF and therefore increments the variable "count" when reaching the end of file, so I ended up with this code,


#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc, char * argv[])
{
if(argc < 2)
{
cout << "Drag file onto .exe";
}
ifstream inFile;

for (int i = 1; i < argc; i++)
{
char ch;
inFile.open(argv);
int count = 0;
while(inFile.get(ch))
{
count++; // Not really counting anything.
}
cout << "There is " << count << " letters in " << argv; // Exchange "<file>" with real filename.
inFile.close();
}
cin.get();
return 0;
}


Thank you for the help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!