8 Replies - 871 Views - Last Post: 17 May 2013 - 09:32 PM Rate Topic: -----

#1 cathysd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-May 13

file input output?

Posted 13 May 2013 - 06:37 PM

Okay so I'm definitely not here to ask anyone to do my homework just please explain the homework assignment so that i ca understand I've read the chapter on input outputing files didn't make any sense to me i
looked at some tutorials made a little more sense but the homework assignment just has me stunned like where do i start?? so if someone could just explain it to more clearly or direct to some video that would clear up most of crazy questions thanks this is my last assignment and classes are now over i'm in my finals right now so my only way of communication is through email with my professor but it takes to long!!!! "Write a program to create a file named 13_1.txt if it does not exist if it does exist append new data write 100 integers created randomly into the file using text I/O integers are separated by a space Ignore the "append" and the 100 integer requirements. Open an output file and write as many scores onto the file as the user enters. Close the output file. Then, reopen the file as input and display the score on the screen. Use the eof() stream function to read scores from the keyboard and from the input file. Use functions for all file handling processing. I guess my biggest question would be what does he mean by "write as many scores onto the file as the user enters" the book didn't explain how to get a users input onto a text file?????? and "Use functions for all file handling processing." so he wants me to make a function for file handling???? I'm sooo confused

Is This A Good Question/Topic? 0
  • +

Replies To: file input output?

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: file input output?

Posted 13 May 2013 - 07:06 PM

Quote

I guess my biggest question would be what does he mean by "write as many scores onto the file as the user enters" the book didn't explain how to get a users input onto a text file??????


You can get input into a temporary variable, then write that input to a file.

i.e.
int input = 0;

//get a 100 integers
for(int i=0; i<100; i++) {
   //get user input
   std::cin>>input;

   //write input to file as text
}



Quote

"Use functions for all file handling processing." so he wants me to make a function for file handling???? I'm sooo confused

You can make some functions to make the code more modular, however he is probably refereing to using the standard FILE IO functions available already to you.

Here is a list of C++ file IO functions available

http://www.cplusplus...stream/fstream/
Was This Post Helpful? 1
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1325
  • View blog
  • Posts: 4,551
  • Joined: 19-February 09

Re: file input output?

Posted 13 May 2013 - 08:27 PM

There are also tutorials etc online that may help :

Input/Output with files
Was This Post Helpful? 1
  • +
  • -

#4 cathysd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-May 13

Re: file input output?

Posted 14 May 2013 - 02:23 AM

View Postcathysd, on 13 May 2013 - 06:37 PM, said:

Okay so I'm definitely not here to ask anyone to do my homework just please explain the homework assignment so that i ca understand I've read the chapter on input outputing files didn't make any sense to me i
looked at some tutorials made a little more sense but the homework assignment just has me stunned like where do i start?? so if someone could just explain it to more clearly or direct to some video that would clear up most of crazy questions thanks this is my last assignment and classes are now over i'm in my finals right now so my only way of communication is through email with my professor but it takes to long!!!! "Write a program to create a file named 13_1.txt if it does not exist if it does exist append new data write 100 integers created randomly into the file using text I/O integers are separated by a space Ignore the "append" and the 100 integer requirements. Open an output file and write as many scores onto the file as the user enters. Close the output file. Then, reopen the file as input and display the score on the screen. Use the eof() stream function to read scores from the keyboard and from the input file. Use functions for all file handling processing. I guess my biggest question would be what does he mean by "write as many scores onto the file as the user enters" the book didn't explain how to get a users input onto a text file?????? and "Use functions for all file handling processing." so he wants me to make a function for file handling???? I'm sooo confused





okay so this is what i have can someone tell me if I'm in the ball park or completely off task needed of me PLEASE!!!!
first one
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

    const int MAX_SIZE = 100;

    int main()
    {
    ofstream output;
 
    output.open("EXCERCISE13_1.txt");   

    int list[MAX_SIZE];
    int size;
	cout << "Enter the amount of values you wished to input (max =  100 )\n followed by the actual values:" << endl;
	cin >> size;
	for (int i = 0; i < size; i++)
	{
        cout<<i+1<<". ";
	cin >> list[i];
    output << list[i] << endl;
     }

  output.close();

  cout << "Done" << endl << endl;
  
  system("pause");
  return 0;
}


and the second one 

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ifstream input("EXCERCISE13_1.txt");
    
    if (input.fail())
  {
    cout << "File does not exist" << endl;
    cout << "Exit program" << endl;
    return 0;
  }
    int numList;
    input >> numList;
    while(!input.eof())
    {
    cout << numList << endl;
    input >> numList;
    }
    
    input.close();
    cout << "Done" << endl << endl;
    
    system("pause");
    return 0;  
    }

This post has been edited by JackOfAllTrades: 14 May 2013 - 06:01 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#5 cathysd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-May 13

Re: file input output?

Posted 14 May 2013 - 02:31 AM

sorry i for forgot to put the tags around my code :P/>/>/>
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

    const int MAX_SIZE = 100;

    int main()
    {
    ofstream output;
 
    output.open("EXCERCISE13_1.txt");   

    int list[MAX_SIZE];
    int size;
	cout << "Enter the amount of values you wished to input (max =  100 )\n followed by the actual values:" << endl;
	cin >> size;
	for (int i = 0; i < size; i++)
	{
    cout<<i+1<<". ";
	cin >> list[i];
    output << list[i] << endl;
     }

  output.close();

  cout << "Done" << endl << endl;
  
  system("pause");
  return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 cathysd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-May 13

Re: file input output?

Posted 14 May 2013 - 02:38 AM

and the second one
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ifstream input("EXCERCISE13_1.txt");
    
    if (input.fail())
  {
    cout << "File does not exist" << endl;
    cout << "Exit program" << endl;
    return 0;
  }
    int numList;
    input >> numList;
    while(!input.eof())
    {
    cout << numList << endl;
    input >> numList;
    }
    
    input.close();
    cout << "Done" << endl << endl;
    
    system("pause");
    return 0;  
    }


Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: file input output?

Posted 14 May 2013 - 04:16 AM

I suggest you read the function tutorials contained in my signature. It looks like you need to have at least three functions in your program, not two programs. The three functions would be main(), a function to read the file, and a function to write the file.


Jim
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: file input output?

Posted 14 May 2013 - 05:16 PM

Just a tip, don't assume that fail() means the file doesn't exist.
    if (input.fail())
  {
    cout << "File does not exist" << endl;
    cout << "Exit program" << endl;
    return 0;
  }



The fail bit could be set if EOF is found, file isn't found, permissions, out of file descriptors, ect... Bad error messages make debugging even harder.

If you just want to determine if the file could not be opened, use is_open()

ifstream input("file.txt");
if(!input.is_open()) {
   std::cerr<<"File could not be opened\nGoodbye";
}
else {
   /* read data */
}



If you want to print out a useful error message on why the file could not be opened, then you need to tell the file stream to throw execptions on the fail bit.

ifstream input;
input.execeptions(std::ios::failbit);
try {
   input.open("file.txt");
   /* Read data */
}
catch(std::exception & e) {
   std::cerr<<"File failure because: "<<e.what()<<std::endl;
}



The first example is the more common error check. Using exceptions can be overkill, however it will help make debugging easier if you have a file IO issue.

This post has been edited by jjl: 14 May 2013 - 05:23 PM

Was This Post Helpful? 0
  • +
  • -

#9 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 131
  • Joined: 08-May 13

Re: file input output?

Posted 17 May 2013 - 09:32 PM

Well you want to use fstream and string.

#include <iostream>
#include <fstream> // File Stream
int main()
{ 
     ofstream file; //Initialize ofstream with an object called "file" (or whatever you want)
     file.open("13_1.txt"); // Make or open a file called 13_1.txt
     file.close();
     return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1