thx Spidey,
that really helped me to understand what goes where. What you first said I already knew, but maybe I said it wrong, but the rest was something I definitly needed :-)
And as Elysia told me, I would use std::getline and std::string instead of my char-thing.
When I tried to compile my my program with the new code in function "write_file()" in util.cpp, I get some other errors. I am mainly confused about the "20: error: cin was not declared in this scope". That is line 20, which is my call std::getline (std::cin,filename); which I took from the cplusplus-reference.
Code:
thommy$ g++ switchfileio.cpp util.cpp -o test
util.cpp: In function ‘void write_file()’:
util.cpp:20: error: ‘cin’ was not declared in this scope
util.cpp:27: error: no matching function for call to ‘std::basic_ofstream<char,
std::char_traits<char> >::basic_ofstream(std::string&)’
/usr/include/c++/4.0.0/fstream:574: note: candidates are: std::basic_ofstream
<_CharT, _Traits>::basic_ofstream(const char*,
std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.0.0/fstream:558: note: std::basic_ofstream<_CharT, _Traits>::basic_ofstream()
[with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.0.0/iosfwd:92: note: std::basic_ofstream<char, std::char_traits<char> >
::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char> >&)
thommy$
So I now have 3 files:
util.h:
Code:
#ifndef _UTIL_H
#define _UTIL_H
// prototyping the functions here, they are declared in util.cpp
void write_file();
void read_file();
void appendto_file();
#endif
util.cpp:
Code:
#include <iostream>
#include <fstream>
#include "util.h"
void write_file(){ // my function that is called when the user wants to write a file
std::string text;
std::string filename;
std::cout<<"Please enter your text, end with pressing 'Enter'\n";
std::getline (std::cin,text);
std::cout <<"You entered: " << text << " \n";
std::cout<<"Please enter a filename to name the file your text is saved to: \n";
std::getline (std::cin,filename);
std::cout <<"You entered: " << filename << " \n";
std::ofstream file (filename);
file<<text;
file.close();
std::cout <<"your file " << filename << "was written successfully.\n";
}
void read_file(){
std::cout << "sorry, feature has yet to be coded...\n";
}
void appendto_file(){
std::cout << "coding in progress... please hold the line...\n";
}
and finally, switchfileio.cpp:
Code:
#include <iostream>
#include <fstream>
#include "util.h"
int main(){
int input; // need this one for the switch-case-menu
do { // this is the menu, it works, have tried this with another lesson!
std::cout<<"1. Write something to a file\n";
std::cout<<"2. Read a file and print its content\n";
std::cout<<"3. Append a string to an existing file\n";
std::cout<<"4.Exit\n";
std::cin>>input;
switch (input) {
case 1:
write_file();
break;
case 2:
read_file();
break;
case 3:
appendto_file();
break;
case 4:
std::cout << "Thanks for playing with me\n";
break;
default:
std::cout<<"Error\n";
break;
}
} while (input == 1 || input == 2 || input == 3); // this is to redraw the menu when one operation is done
std::cin.get();
}