-
help me with files
My program needs to do the following things
Asks the user of the name of the input file
If the user types an incorrect file name (a file name that does not exists in that directory), your code will print the appropriate message and terminate
If the user types the correct file name your program reads the numbers from that file one by one while it keeps a running total
You close that file
You ask the user where he wants the results to be stored
The user types a file name
Your program opens that file for output
Your program writes in that file the result that you had already calculated in step 4
Close the output file
Inform the user that the task was completed
When the user hits ENTER terminate the program
I need help with the validation of the file to check, how to do the count, and using the files that the user wants..
HELP ME PLEASE!!
Code:
#include <iostream.h>
#include <fstream.h>
#include <ios.h>
int main()
{
char f;
int number;
int count=1;
ifstream file;
cout << "What is the name of the file you want to read from?"<<endl;
cin >> f;
file.open("num.txt", ios::nocreate);
if (file.fail())
cout << "error opening file";
else
{
while (count++)
{
file >> number;
count++;
}
cout << count;
file.close();
}
return 0;
}
-
You probably want to read a string, rather than a single character. If you use the "std::string" or a char array is up to you.
You don't seem to suplly any code for this:
You ask the user where he wants the results to be stored
The user types a file name
Your program opens that file for output
Your program writes in that file the result that you had already calculated in step 4
Close the output file
Inform the user that the task was completed
When the user hits ENTER terminate the program
You are counting the number of numbers in the file [incorrectly, as you count each number twice] - and you don't actually detect when the end of file is reached. So this part of your task is incorrect.
If the user types the correct file name your program reads the numbers from that file one by one while it keeps a running total
Your file validation seems to be correct to me. Does it work? [Obviously, you are currently using a constant in your open() call, "num.txt", and that would need to change so that it uses a variable that the user inputs - like the f variable once you've made it into a string]
--
Mats
-
changed a few things just now
Code:
//
//
//
//
#include <iostream.h>
#include <fstream.h>
#include <ios.h>
int main()
{
char inf[50];
char outf[50];
int number;
int count=1;
ifstream infile;
ofstream outfile;
cout << "What is the name of the file you want to read from?"<<endl;
cin >> inf;
infile.open(inf, ios::nocreate);
if (infile.fail())
cout << "error opening file";
else
{
while (count<=number)
{
infile >> number;
count++;
}
cout << count;
infile.close();
}
cout << "what file do you want me to store the count in?";
cin >> outf;
outfile.open(outf, ios::nocreate);
if (!outfile)
{
cout << "error opening file";
return 0;
}
else
{
outfile << count;
}
return 0;
}
-
I'm pretty sure that your result should be the sum of the numbers in the file, and your read-loop is still broken on two accounts:
1. It doesn't detect failing to read.
2. It counts up twice (once inside the while condition and once in the loop itself).
--
Mats
-
so how would i change it so that it only counts once.. also no matter how many numbers are in the file (say theres 42) i always get 1 as count.. how would i make the while condition work to count the amount of numbers (separated by white spaces) and make it only count once..? what would the condition be?
-
Ah, yes, I didn't think it through very far - if your count is zero the first time round, then you don't even enter the loop, so nothing is ever read from the file.
So, what do you think you should do if:
1. You want to read a number from the file for as long as it's possible.
2. Sum up the numbers you read into a sum variable.
[You can count the number of them too if you like, but that's not what I thought the task was supposed to do].
--
Mats
-
umm im not really sure. i know i need to count the amount of numbers. so count++ in the while loop should be enough, im just really confused as to what condition i would use to make the while loop work until the end of file marker and then store that variable and write it to whichever file the uses pleases
-
First of all, those are two distinct problems:
- Produce the sum of all the numbers. [There is absolutely no requirement that you have given that says "Produce a count of the numbers"].
- Write the data to a user-specified output file. The code you have is good for asking the user and creating a file - all that remains is to produce the data [whcih of course depends on the above].
You may find this helpful:
When you use a statement like:
infile >> number;
it produces a "result" of "success" or "fail", where "fail" is "false".
--
Mats
-
i can get my code to write to the file but it doesnt keep an accurate count.. i keep getting trash values such as: 536870912 Depending on what i have in the input file it changes but its always a trash value.. i think the problem may be the condition in the while loop.. but im unsure.. help me fix this problem please
code is as follows..
Code:
//
//
//
//
#include <iostream.h>
#include <fstream.h>
#include <ios.h>
int main()
{
char inf[50];
char outf[50];
int number;
int count=1;
ifstream infile;
ofstream outfile;
cout << "What is the name of the file you want to read from?"<<endl;
cin >> inf;
infile.open(inf, ios::nocreate);
if (infile.fail())
cout << "error opening file";
else
{
while (count << number)
{
infile >> number;
count++;
}
infile.close();
}
cout << "what file do you want me to store the count in?";
cin >> outf;
outfile.open(outf, ios::nocreate);
if (outfile.fail())
{
cout << "error opening file";
}
else
{
outfile << count;
}
outfile.close();
return 0;
}
-
Code:
while (count << number)
will shift your count by "number" bits to the left as a binary operator. This is NOT what you want to do here. In my example, the left-hand operand of the << operator is a stream, not an integer variable - this is one of the tricky things in C++ - something that looks like it does one thing with simple variables can do something completely different with OTHER inputs.
--
Mats
-
so what should the condition be
-
What do you think it should be? Something where the left operand of the >> is a filestream, right? And what are you doing using a filestream already?
--
Mats
-
honestly i just saw something in my textbook that looked better than what i had.... im totally in the dark right now and just need somewhere to go.... i know that i want the while loop to say... while there are still numbers to be read, read the number then do count++
-
So look at your code, and look at what the types are. Do you find anything that is a filestream of some sort?
--
Mats
-
infile is a file stream so is outfile... i think