Search Results

Search found 209 results on 9 pages for 'fstream'.

Page 1/9 | 1 2 3 4 5 6 7 8 9  | Next Page >

  • Using a macro for fstream file input as part of a class

    - by vette982
    I have a class that processes a file, and as part of the constructor with one argument I want to input a file using fstream. I basically want it do something like this class someClass{ public: someClass(char * FILENAME) { fstream fileToProcess; fileToProcess.open(<FILENAME>, fstream::in | fstream::out | fstream::app); } }; I want to pass the filename in as an argument to the class constructor, and then the class someClass will access it with fstream.

    Read the article

  • Load binary file using fstream

    - by Kirill V. Lyadvinsky
    I'm trying to load binary file using fstream in the following way: #include <iostream #include <fstream #include <iterator #include <vector using namespace std; int main() { basic_fstream<uint32_t file( "somefile.dat", ios::in|ios::binary ); vector<uint32_t buffer; buffer.assign( istream_iterator<uint32_t, uint32_t( file ), istream_iterator<uint32_t, uint32_t() ); cout << buffer.size() << endl; return 0; } But it doesn't work. In Ubuntu it crashed with std::bad_cast exception. In MSVC++ 2008 it just prints 0. I know that I could use file.read to load file, but I want to use iterator and operator>> to load parts of the file. Is that possible? Why the code above doesn't work?

    Read the article

  • Strange fstream problem

    - by noname
    Hi, I have really strange problem. In Visual C++ express, I have very simple code, just: #include <fstream> using namespace std; int main() { fstream file; file.open("test.txt"); file<<"Hello"; file.close(); } This same code works OK in my one project, but when I create now project and use this same lines of code, no file test.txt is created. Please, what is wrong?¨ EDIT: I expect to see test.txt in VS2008/project_name/debug - just like the first functional project does.

    Read the article

  • fstream stopping math.h from working

    - by CaptainProg
    I am creating a program in C++ in which I need to read a text file in. I have included the fstream header file, which allows me to open the file, but having added the include, I now receive countless errors relating to math.h functions. Examples: 1>c:\program files\microsoft visual studio 10.0\vc\include\cmath(19): error C2061: syntax error : identifier 'acosf' 1>c:\program files\microsoft visual studio 10.0\vc\include\cmath(19): error C2059: syntax error : ';' Is there any way I can include the text file reading functions of fstream without compromising the math.h functions? And why does this conflict occur anyway? /Edit/ It seems the errors are in the cmath standard header file. It is nothing I have access to, but for the sake of completion, here is the code that is causing the errors: using _CSTD acosf; using _CSTD asinf; using _CSTD atanf; using _CSTD atan2f; using _CSTD ceilf; (etcetera)

    Read the article

  • fstream file I/O question

    - by Konrad
    Hi, I am trying to work out if I need to call close on a fstream object if the intial open failed. i.e. std::fstream strm; strm.open( "filename" ); if( ! strm.fail() ) { // Do something strm.close(); // [1] } strm.close(); // [2] Where should close be called here - should it always be called [2] or only if the open succeeds[1]? I may be going over the top here, but coming from the Windows API way of typically doing this I have CloseHandle( ... ); embedded in my mind :-)

    Read the article

  • fstream - correct error checking after output

    - by Truncheon
    What's the correct way to check for a general error when sending data to an fstream? I have the following code, which seems a bit overkill. int Saver::output() { save_file_handle.open(file_name.c_str()); if (save_file_handle.is_open()) { save_file_handle << save_str.c_str(); if (save_file_handle.bad()) { x_message("Error - failed to save file"); return 0; } save_file_handle.close(); if (save_file_handle.bad()) { x_message("Error - failed to save file"); return 0; } return 1; } else { x_message("Error - couldn't open save file"); return 0; } }

    Read the article

  • C++ Newbie: Passing an fstream to a function to read data

    - by vgm64
    I have a text file named num.txt who's only contents is the line 123. Then I have the following: void alt_reader(ifstream &file, char* line){ file.read(line, 3); cout << "First Time: " << line << endl; } int main() { ifstream inFile; int num; inFile.open("num.txt"); alt_reader(inFile, (char*)&num); cout << "Second Time: " << num << endl; } The output is: First Time: 123 Second Time: 3355185 Can you help me figure out how to get an fstream that is read in a function still assign the variable in main? I'm doing this because alt_reader really has a lot more to it, but this is the part I'm stuck on. Thanks a lot for the help.

    Read the article

  • When is using stdio preferable to fstream?

    - by Karl Bielefeldt
    I work on a well-established, embedded C++ code base. We have been using a proprietary API to our filesystem. For better integration with third-party C libraries, we are currently in the process of implementing most of stdio.h and fcntl.h. I made what I thought was a non-controversial proposal that we should also implement the fstream class and encourage new C++ code to use it instead of the new (to our code base) C-style API. We already have the stdout parts of iostream available, although it is not widely used. Given a choice between using stdio and fstream, what are good reasons to choose stdio for embedded software development in C++?

    Read the article

  • What's the correct way to do a "catch all" error check on an fstream output operation?

    - by Truncheon
    What's the correct way to check for a general error when sending data to an fstream? UPDATE: My main concern regards some things I've been hearing about a delay between output and any data being physically written to the hard disk. My assumption was that the command "save_file_obj << save_str" would only send data to some kind of buffer and that the following check "if (save_file_obj.bad())" would not be any use in determining if there was an OS or hardware problem. I just wanted to know what was the definitive "catch all" way to send a string to a file and check to make certain that it was written to the disk, before carrying out any following actions such as closing the program. I have the following code... int Saver::output() { save_file_handle.open(file_name.c_str()); if (save_file_handle.is_open()) { save_file_handle << save_str.c_str(); if (save_file_handle.bad()) { x_message("Error - failed to save file"); return 0; } save_file_handle.close(); if (save_file_handle.bad()) { x_message("Error - failed to save file"); return 0; } return 1; } else { x_message("Error - couldn't open save file"); return 0; } }

    Read the article

  • Getting meaningful error messages from fstream's in C++

    - by Hassan Syed
    What is the best way to get meaningful file access error messages, in a portable way from std::fstreams ? The primitiveness of badbits and failbits is getting to be bit annoying. I have written my own exception hierarchies against win32 and POSIX before, and that was far more flexible than the way the STL does it. I am getting "basic::ios_clear" as an error message from the what method of a downcasted catch (std::exception) of a fstream which has exceptions enabled. This doesn't mean much to me, although I do know what the problem is I'd like my program to be a tad more informative so that when I start deployment a few months later my life will be easier. Is there anything in Boost to extract meaningful messages out of the ofstream's implementation cross platform and cross STL implementation ?

    Read the article

  • filebuf in ifstream and ofstream

    - by Juan
    How can i set a new char array to be the buffer of a fstream's filebuf, there is a function (setbuf) in the filebuf but it is protected. while searching on the web, some sites mention fstream::setbuf but it doesn't seem to exist anymore. Thanks

    Read the article

  • C++ File manipulation problem

    - by Carlucho
    I am trying to open a file which normally has content, for the purpose of testing i will like to initialize the program without the files being available/existing so then the program should create empty ones, but am having issues implementing it. This is my code originally void loadFiles() { fstream city; city.open("city.txt", ios::in); fstream latitude; latitude.open("lat.txt", ios::in); fstream longitude; longitude.open("lon.txt", ios::in); while(!city.eof()){ city >> cityName; latitude >> lat; longitude >> lon; t.add(cityName, lat, lon); } city.close(); latitude.close(); longitude.close(); } I have tried everything i can think of, ofstream, ifstream, adding ios::out all all its variations. Could anybody explain me what to do in order to fix the problem. Thanks!

    Read the article

  • Why do I get corrupt output on my file?

    - by Martin
    I have a simple program which I have compiled in both MinGW and Visual C++ 2008 Express, and both give an output file larger than 88200. When I set s = 0, both programs work as expected. What am I doing wrong? #include <fstream> using namespace std; int main(int argc, char *argv[]) { int i; short s; fstream f; f.open("test.raw", ios_base::out); for(i = 0; i < 44100; i++) { s = i & 0xFFFF; // PROBLEM? f.write(reinterpret_cast<const char *>(&s), sizeof(s)); } f.close(); return 0; }

    Read the article

  • Problem with reading file line-by-line

    - by Maulrus
    I'm trying to complete an exercise to write a program that takes the following command line arguments: an input file, an output file, and an unspecified number of words. The program is to read the contents of the input file line by line, find for each word given which lines contain the word, and print the lines with their line number to the output file. Here's my code: #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; int main(int argc, char* argv[]) { if (argc < 4) { cerr << "Error #1: not enough arguments provided\n"; return 1; } ifstream in(argv[1]); if (!in.is_open()) { cerr << "Error #2: input file could not be opened\n"; return 2; } ofstream out(argv[2]); if (!out.is_open()) { cerr << "Error #3: output file could not be opened\n"; return 3; } ostringstream oss; for (int i = 3; i < argc; ++i) { int k = 0; string temp; oss << argv[i] << ":\n\n"; while (getline(in, temp)) { ++k; unsigned x = temp.find(argv[i]); if (x != string::npos) oss << "Line #" << k << ": " << temp << endl; } } string copy = oss.str(); out << copy; in.close(); out.close(); return 0; } If I try to run that, I get the predicted output for the first word given, but any words following it aren't found. For example, for the source code above will give the following output: in: Line #1: #include <iostream> Line #2: #include <fstream> Line #3: #include <string> Line #4: #include <sstream> Line #5: using namespace std; Line #7: int main(int argc, char* argv[]) { Line #12: ifstream in(argv[1]); Line #13: if (!in.is_open()) { Line #14: cerr << "Error #2: input file could not be opened\n"; Line #22: ostringstream oss; Line #23: string temp; Line #24: for (int i = 3; i < argc; ++i) { Line #26: int k = 0; Line #28: while (getline(in, temp)) { Line #30: unsigned x = temp.find(argv[i]); Line #31: if (x != string::npos) Line #32: oss << "Line #" << k << ": " << temp << endl; Line #35: string copy = oss.str(); Line #37: in.close(); out: That is, it'll find all the instances of the first word given but not any following. What am I doing wrong here?

    Read the article

  • C++ iostream not setting eof bit even if gcount returns 0

    - by raph.amiard
    Hi I'm developping an application under windows, and i'm using fstreams to read and write to the file. I'm writing with fstream opened like this : fs.open(this->filename.c_str(), std::ios::in|std::ios::out|std::ios::binary); and writing with this command fs.write(reinterpret_cast<char*>(&e.element), sizeof(T)); closing the file after each write with fs.close() Reading with ifstream opened like this : is.open(filename, std::ios::in); and reading with this command : is.read(reinterpret_cast<char*>(&e.element), sizeof(T)); The write is going fine. However, i read in a loop this way : while(!is.eof()) { is.read(reinterpret_cast<char*>(&e.element), sizeof(T)); } and the program keeps reading, even though the end of file should be reached. istellg pos is 0, and gcount is equal to 0 too, but the fail bit and eof bit are both ok. I'm running crazy over this, need some help ...

    Read the article

  • vector segmentation fault

    - by user1849298
    I have a problem with the segmentation fault. Look: #include<fstream> using namespace std; int main(){ int n,i,vector[10001],vectorcopy[10001]; ifstream in("program.in"); ofstream out("program.out"); in>>n; for(i=1;i<=n;i++){ in>>vector[i]; vectorcopy[i]=vector[i]; } return 0;} And the debugger says: Program recived signal SIGSEGV, Segmentation fault Please, tell me what to do!

    Read the article

  • c++: how can i read a file line by line to a string type variable?

    - by ufk
    Hiya. I'm trying to read a file line by line to a string type variable using the following code: #include <iostream> #include <fstream> ifstream file(file_name); if (!file) { cout << "unable to open file"; exit(1); } string line; while (!file.eof()) { file.getline(line,256); cout<<line; } file.close(); it won't compile when I try to use String class, only when i use char file[256] instead. how can I get line by line into a string class? thanks!

    Read the article

  • How do you search a document for a string in c++?

    - by Jeff
    Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() { int count = 0; string fileName; string keyWord; string word; cout << "Please make sure the document is in the same file as the program, thank you!" << endl << "Please input document name: " ; getline(cin, fileName); cout << endl; cout << "Please input the word you'd like to search for: " << endl; cin >> keyWord; cout << endl; ifstream infile(fileName.c_str()); while(infile.is_open()) { getline(cin,word); if(word == keyWord) { cout << word << endl; count++; } if(infile.eof()) { infile.close(); } } cout << count; } I'm not sure how to go to the next word, currently this infinite loops...any recommendation? Also...how do I tell it to print out the line that that word was on? Thanks in advance!

    Read the article

  • Possible reasons for tellg() failing?

    - by Andreas Bonini
    ifstream::tellg() is returning -13 for a certain file. Basically, I wrote a utility that analyzes some source code; I open all files alphabetically, I start with "Apple.cpp" and it works perfectly.. But when it gets to "Conversion.cpp", always on the same file, after reading one line successfully tellg() returns -13. The code in question is: for (int i = 0; i < files.size(); ++i) { /* For each .cpp and .h file */ TextIFile f(files[i]); while (!f.AtEof()) // When it gets to conversion.cpp (not on the others) // first is always successful, second always fails lines.push_back(f.ReadLine()); The code for AtEof is: bool AtEof() { if (mFile.tellg() < 0) FATAL(format("DEBUG - tellg(): %d") % mFile.tellg()); if (mFile.tellg() >= GetSize()) return true; return false; } After it reads successfully the first line of Conversion.cpp, it always crashes with DEBUG - tellg(): -13. This is the whole TextIFile class (wrote by me, the error may be there): class TextIFile { public: TextIFile(const string& path) : mPath(path), mSize(0) { mFile.open(path.c_str(), std::ios::in); if (!mFile.is_open()) FATAL(format("Cannot open %s: %s") % path.c_str() % strerror(errno)); } string GetPath() const { return mPath; } size_t GetSize() { if (mSize) return mSize; const size_t current_position = mFile.tellg(); mFile.seekg(0, std::ios::end); mSize = mFile.tellg(); mFile.seekg(current_position); return mSize; } bool AtEof() { if (mFile.tellg() < 0) FATAL(format("DEBUG - tellg(): %d") % mFile.tellg()); if (mFile.tellg() >= GetSize()) return true; return false; } string ReadLine() { string ret; getline(mFile, ret); CheckErrors(); return ret; } string ReadWhole() { string ret((std::istreambuf_iterator<char>(mFile)), std::istreambuf_iterator<char>()); CheckErrors(); return ret; } private: void CheckErrors() { if (!mFile.good()) FATAL(format("An error has occured while performing an I/O operation on %s") % mPath); } const string mPath; ifstream mFile; size_t mSize; }; Platform is Visual Studio, 32 bit, Windows. Edit: Works on Linux. Edit: I found the cause: line endings. Both Conversion and Guid and others had \n instead of \r\n. I saved them with \r\n instead and it worked. Still, this is not supposed to happen is it?

    Read the article

  • Returning ifstream in a function

    - by wrongusername
    Here's probably a very noobish question for you: How (if at all possible) can I return an ifstream from a function? Basically, I need to obtain the filename of a database from the user, and if the database with that filename does not exist, then I need to create that file for the user. I know how to do that, but only by asking the user to restart the program after creating the file. I wanted to avoid that inconvenience for the user if possible, but the function below does not compile in gcc: ifstream getFile() { string fileName; cout << "Please enter in the name of the file you'd like to open: "; cin >> fileName; ifstream first(fileName.c_str()); if(first.fail()) { cout << "File " << fileName << " not found.\n"; first.close(); ofstream second(fileName.c_str()); cout << "File created.\n"; second.close(); ifstream third(fileName.c_str()); return third; //compiler error here } else return first; } EDIT: sorry, forgot to tell you where and what the compiler error was: main.cpp:45: note: synthesized method ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)’ first required here EDIT: I changed the function to return a pointer instead as Remus suggested, and changed the line in main() to "ifstream database = *getFile()"; now I get this error again, but this time in the line in main(): main.cpp:27: note: synthesized method ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)’ first required here

    Read the article

1 2 3 4 5 6 7 8 9  | Next Page >