473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

File I/O

Hi!
I need to write a program that reads information about youth soccer teams from a file, calculates the average score for
each team and prints the averages in a neatly formatted table along with the team name.
My problem is these input and output files. I know I have to use <fstream> and

infile.open
outfile.open

infile.close
outfile.close

When using these, does the ".txt" file have to print out in my output? How does it work?
Sep 11 '07 #1
5 1133
ilikepython
844 Expert 512MB
Hi!
I need to write a program that reads information about youth soccer teams from a file, calculates the average score for
each team and prints the averages in a neatly formatted table along with the team name.
My problem is these input and output files. I know I have to use <fstream> and

infile.open
outfile.open

infile.close
outfile.close

When using these, does the ".txt" file have to print out in my output? How does it work?
I dont really know what you are talking about. If your text file's name is "file.txt":
Expand|Select|Wrap|Line Numbers
  1. infile.open("file.txt");
  2.  
Sep 11 '07 #2
I dont really know what you are talking about. If your text file's name is "file.txt":
Expand|Select|Wrap|Line Numbers
  1. infile.open("file.txt");
  2.  
I am supposed to create a file ( ex: "input.txt ) in my project directory and the program has to read the data from that file. This is what I tried and it does not work.
Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <iostream>
  3.  
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8.  
  9. {
  10.        ofstream myOutfile;
  11.        myOutfile.open("input.txt");
  12.        if(!myOutfile)
  13.  
  14.  
  15.     {
  16.  
  17.         cout << "File did not open";
  18.             exit(1);
  19.  
  20.  
  21.     }
  22.  
  23.     int input = 10;
  24.     myOutfile << input;
  25.  
  26.     myOutfile.close();
  27.  
  28.  
  29.     cin.get();
  30.     cin.get();
  31.  
  32.  
  33.  
  34.  
  35. }
Sep 11 '07 #3
ilikepython
844 Expert 512MB
I am supposed to create a file ( ex: "input.txt ) in my project directory and the program has to read the data from that file. This is what I tried and it does not work.
#include <fstream>
#include <iostream>


using namespace std;

int main()

{
ofstream myOutfile;
myOutfile.open("input.txt");
if(!myOutfile)


{

cout << "File did not open";
exit(1);


}

int input = 10;
myOutfile << input;

myOutfile.close();


cin.get();
cin.get();




}
Try using:
Expand|Select|Wrap|Line Numbers
  1. if (myOutfile.is_open())
  2. {
  3. }
  4.  
Also, make sure the file is in the same directory as the program otherwise you have to use an absolute path.
Sep 12 '07 #4
Ganon11
3,652 Expert 2GB
But since he's using an ofstream, if the text file doesn't exist, shouldn't it create one for him named input.txt?
Sep 12 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
I'm confused here:

[quote=kateryna]
I am supposed to create a file ( ex: "input.txt ) in my project directory and the program has to read the data from that file. This is what I tried and it does not work.
[//quote]

If the program has to read from the file, why is it opened as an output file?

Isn't this a case where you create input.txt with an editor and then read it as an ifstrream???
Sep 12 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
7
by: Joseph | last post by:
Hi, I'm having bit of questions on recursive pointer. I have following code that supports upto 8K files but when i do a file like 12K i get a segment fault. I Know it is in this line of code. ...
3
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
3
by: Shapper | last post by:
Hello, I created a script to upload a file. To determine the file type I am using userPostedFile.ContentType. For example, for a png image I get "image/png". My questions are: 1. Where can...
0
by: troutbum | last post by:
I am experiencing problems when one user has a document open through a share pointing to the web site. I use the dsolefile to read the contents of a particular directory and then display them in a...
0
by: thjwong | last post by:
I'm using WinXP with Microsoft Visual C++ .NET 69462-006-3405781-18776, Microsoft Development Environment 2003 Version 7.1.3088, Microsoft .NET Framework 1.1 Version 1.1.4322 SP1 Most developers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.