Thread: help me with files

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    24

    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;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Last edited by matsp; 11-05-2007 at 08:56 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    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;
    }
    Last edited by xclarkiex5x; 11-05-2007 at 09:09 AM. Reason: didnt compile

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    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?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    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

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    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;
    }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    so what should the condition be

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    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++

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So look at your code, and look at what the types are. Do you find anything that is a filestream of some sort?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    infile is a file stream so is outfile... i think

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM