473,396 Members | 1,996 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.

ifstream ofstream help

I need to make a program where it can read from a file and write to it. But the problem is, all i can do is read from it and not write to it. This is the code i have so far:

Expand|Select|Wrap|Line Numbers
  1. # include <iostream>
  2. # include <fstream>
  3. # include <cstdlib>
  4. # include <iomanip>
  5. # include <string>
  6. # include <vector>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     string filePath = "C:\\documents\\test\\";
  13.     string fileName = "";
  14.     string name = "";
  15.     char character=' ';
  16.  
  17.     ofstream myfile("C:\\documents\\test\\");
  18.     ifstream inputFile;
  19.  
  20.     vector<string>names(4," ");
  21.  
  22.     cout << "Please enter the name of the input file: ";
  23.     getline(cin, fileName);
  24.     fileName = filePath + fileName;
  25.  
  26.     inputFile.open(fileName.c_str());
  27.  
  28.     while(inputFile.get(character)) 
  29.     {
  30.         cout << character;
  31.     }
  32.  
  33.     cin.get();
  34.     cin.get();
  35.     return 0;
  36. }
  37.  
Mar 23 '07 #1
5 2649
sicarie
4,677 Expert Mod 4TB
That's because you just declare it as an ifstream (input file stream), and you want it to be an fstream to both read and write to it.

I personally like cplusplus.com's tutorials, I think they're easy to read and have good examples, but let me know if that doesn't help.
Mar 23 '07 #2
OK i get it, thanks. But here is the real problem: The program should ask the user where is a text file. The file should open and it will be a list of names. The names should be stored in a vector after being read by the program.The program then will call a function named print that prints to the monitor the list of names, numbered starting with 1. It will ask the user if they want to delete a name, insert a name, or write the names to a file and end the program. The problem here is, i can get the file to open up and display the names, but i can't get it to prompt the user what to do next. It basically just displays the file, but doesnt allow for any editing.
Expand|Select|Wrap|Line Numbers
  1. # include <iostream>
  2. # include <fstream>
  3. # include <cstdlib>
  4. # include <iomanip>
  5. # include <string>
  6. # include <vector>
  7.  
  8. using namespace std;
  9.  
  10. int choice=0;
  11.  
  12. int main()
  13. {
  14.     string filePath = "C:\\17\\test\\";
  15.     string fileName = "";
  16.     string name = "";
  17.     char character=' ';
  18.  
  19.     ofstream myfile("C:\\17\\test\\");
  20.     ifstream inputFile;
  21.  
  22.     vector<string>names(4," ");
  23.  
  24.     cout << "Please enter the name of the input file: ";
  25.     getline(cin, fileName);
  26.     fileName = filePath + fileName;
  27.  
  28.     inputFile.open(fileName.c_str());
  29.  
  30.     while(inputFile.get(character)) 
  31.     {
  32.         cout << character;
  33.         cout<<endl;
  34.     }
  35.  
  36.     cout<<"Do you want to:";
  37.     cout<<endl;
  38.     cout<<"1. Delete a name";
  39.     cout<<endl;
  40.     cout<<"2. Insert a name";
  41.     cout<<endl;
  42.     cout<<"3. Write the names to a file and end the program";
  43.     cout<<endl;
  44.     cin>>choice;
  45.  
  46.  
  47.     cin.get();
  48.     cin.get();
  49.     return 0;
  50. }
  51.  
Yes, i am a beginner at C++
Mar 25 '07 #3
sicarie
4,677 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1.  
  2.     cout<<"Do you want to:";
  3.     cout<<endl;
  4.     cout<<"1. Delete a name";
  5.     cout<<endl;
  6.     cout<<"2. Insert a name";
  7.     cout<<endl;
  8.     cout<<"3. Write the names to a file and end the program";
  9.     cout<<endl;
  10.     cin>>choice;
  11.  
  12.  
  13.     cin.get();
  14.     cin.get();
  15.     return 0;
  16. }
  17.  
You have no control structure for keeping in the program, you just get two inputs and exit. I would recommend using a while loop around the code above, and then you could call whatever functions you wanted to edit or save the vector.
Mar 25 '07 #4
I know that, thanks, but the problem is how to store the names in the vector after the file is opened.
Mar 25 '07 #5
sicarie
4,677 Expert Mod 4TB
I know that, thanks, but the problem is how to store the names in the vector after the file is opened.
Expand|Select|Wrap|Line Numbers
  1.     while(inputFile.get(character)) 
  2.     {
  3.         cout << character;
  4.         cout<<endl;
  5.     }
  6.  
You do it there, add each filename to the vector, while you're reading it in, that way you can close your ifstream when you're done with it. I would recommend using getline again, and then if there are multiple elements on one line, strtok() to parse those elements apart.
Mar 25 '07 #6

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

Similar topics

2
by: Armando | last post by:
i write one programm in vc++ but the compiler say that i use ambiguous symbol,i don´t know why ? how can do to tell the compiler that is ok so ? i become this error message: error C2872:...
4
by: Clint Ruen | last post by:
Hello all, I have written out a data structure using the binary flag on an ofstream. The struct/class is something like this class SomeData { public: int data1;
1
by: phoenix | last post by:
How can i read a numbers from file.. I created this file like this: int dane, i; std::ofstream plik_z; plik_z.open(nazwa, std::ios::out | std::ios::trunc | std::ios::binary); for (i = 0; i <...
7
by: shawn | last post by:
Hi All, Am using MSVC6 compiler on a WinXP machine. Am trying to read a file using std::ofstream; the problem is that Tofstream.is_open() fails and Tifstream.rdstate() returns "2" which...
2
by: Karl | last post by:
Hey everyone! I've got a quick question on whether std::ifstream is buffered or not. The reason is that I have a homework assignment that requires me to benchmark copying files using different...
4
by: manontheedge | last post by:
i'm working on a C++ program that uses operator overloads (which i'm just learning to use), and i have the ostream working fine, but i'm thinking i'm using it wrong somehow because the ofstream...
4
by: marathoner | last post by:
I tried your advice, and replaced "ifstream" with "std::ifstream". I also replaced instances of "ofstream" with "std::ofstream". Those syntax errors were resolved. When I added "std" to the...
6
by: Gary Wessle | last post by:
hi I have a code, the part which is troubling goes like this **************************************************************** #include <istream> #include <ostream> #include <fstream>
11
by: adramolek | last post by:
So... I'm trying to get used to using C++ ifstream (or ofstream) instead of stdio (although I'm having second thoughts). Anyways, I want to be able to display a meaningful error message if ifstream...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.