I wonder why this program does not do any changes to the text file that I am trying to modify. Scenario: My program uses the Fstream header file, I declared infile as IFSTREAM and outfile as OFSTREAM. I simply want to transfer data from infile to outfile. The data is as follows; Adam Larson 89 90 This data is saved in a text file manually created with Notepad. Furthermore, I also want to add the average of 89 and 90 into the outfile target file however, it doesn't do it. it does nothing at all. I also observed that there …

Member Avatar
Member Avatar
+0 forum 4

hello again, so i want to make a programme where the file saved is saved with different names at every loop.. like at first loop it will save as file1.txt, 2nd loop file2.txt, so on... so this is my code... #include <iostream> #include <fstream> using namespace std; int main() { int x = 0; cout << "Hi" << endl; for (x == 0; x<10; x++){ fstream file; file.open("trial", ios::out); file<<"save it"; file.close(); } cin.ignore(); cout<<" DONE"; return 0; } so this is what the code is, it will loop 10 times and it will save it as file1.txt, file2.txt, file3.txt......... …

Member Avatar
Member Avatar
+0 forum 1

Dear Kind DaniWebbers, I am trying to overload the fstream operators (ofstream/ifstream), so that I can save a class to a 'Binary' file and also display it with cout. But the ways in which each need to be implemented is different. Please can you show me how to define different operators for each. Here is my test class: class CBase { public: //CBase(void) {}; CBase(int i = 0, float f = float(0.0), double d = double(0.0), char c = 'A') : iMyInt(i), fMyFloat(f), dMyDouble(d), cMyChar(c) {} ~CBase(void) {} int iMyInt; float fMyFloat; double dMyDouble; char cMyChar; friend std::ostream& operator<<(std::ostream& os, …

Member Avatar
Member Avatar
+0 forum 4

#Include<fstream> #include<> #include<iostream> #include<string> using namespace std; int main() { string str="time is a great teacher, but unfortunatly" "it kills all its puplis. Berlioz"; ofstream outfile("ali.TXT");//ERROR for(int j=0;j<str.size();j++) outfile.put(str[j]); cout<<"file written\n"; //system("pause"); return 0; } please correct my code"ofstream outfile("ali.txt")"

Member Avatar
Member Avatar
+0 forum 1

I'm writing a program that will keep track of every Anime/Manga I watch/read(I'm an avid fan). I'm trying to make a database in order to keep track of all of the titles of the Anime/Manga. My problem is that when i try to add a new title to the existing list, it overwrites the entries that are already there. Here is the code for my add function. int Add(void) { outfile.open("database.txt"); cout << "What is the title of the Anime/Manga that you wish to add?" << endl; cout << "::>"; cin >> Title; outfile << Title << endl; outfile.close(); } …

Member Avatar
Member Avatar
+0 forum 2

I am trying to write a program that creates a new file every time I use the application. Right now, this is what I am doing: outfile.open("Application.txt"); outfile<<"First Name: "<< firstName <<" "<<"Last Name: "<< lastName<<" "<<"Student Number: "<< studentNumber << endl; outfile<<"Email Address: " << emailAddress <<" "<< "Major: " << major << " " <<"Year standing: "<< yearStanding << endl; outfile<<"CGPA: "<< culmativeGPA <<" "<<"MajorGPA: "<< majorGPA << endl; outfile.close(); What I want to actually do, is check if application001.txt exists, and if it doesn't, create that file then write to it, and then close it. If it …

Member Avatar
Member Avatar
+0 forum 3

It seems I don't understand completely reading from and into files, line by line. Here is my code that is supposed to read lines of two file, into string variables, I assume, and then write them into third file: *line of first file line of second file .... so on if one of files ends, the rest of the other is added to the end* Where is my mistakes? it compiled, but did not write anything into third file #include <iostream> #include <string> #include <fstream> using namespace std; int main () { string name1, name2, line1, line2, name3; cin>>name1; cin>>name2; …

Member Avatar
Member Avatar
+0 forum 3

Can some one briefly tell me about that how to do fileprocessing in c++

Member Avatar
Member Avatar
+0 forum 2

Here I have the classes Logger and Driver. Driver takes the singleton class of Logger, and logs the same statement 10000 times. I want to use this logger, so that it runs in the background and only logs every so often. This will force the program to not use too much cpu time to log data, but rather other crucial tasks. #pragma once #include <deque> #include <string> #include <fstream> #include <map> #include <iostream> #include <pthread.h> #include <time.h> #define MAX_LINES 10000 #define MESSAGES_PER_WRITE 101 namespace App { class Logger { friend class Driver; friend class OrderManager; public: static Logger* Instance(); void …

Member Avatar
Member Avatar
+0 forum 5

int Finish()//outputs stored values to file { ofstream walletfile; walletfile.open("C:/accounts/wallet.acnt", ios::trunc); walletfile << wallet; walletfile.close(); ofstream accountfile; accountfile.open("C:/accounts/account.acnt", ios::trunc); accountfile << account; accountfile.close(); ofstream savingsfile; savingsfile.open("C:/accounts/savings.acnt", ios::trunc); savingsfile << savings; savingsfile.close(); return 0; } I am a brand new programmer, just learning from the web, i wrote this function as part of an accounting program to write stored values to an output file, the program compiles, and runs without any problems, but when i activate this function from within the program nothing happens, when i retrieve the values from the file, they are exactly the same as the last time …

Member Avatar
Member Avatar
+0 forum 12

a friend asked me to help him with his lab. the lab involves opening a file and adding the numbers in the file together displaying how many even and how many odd numbers there are. I have the file "textinput.txt" in the directory with the .cpp file but when i build i still get the error message. I don't get why. I'm using XCode if that helps. Did i forget to include a correct library or is there something about how XCode reads the ifstream.open that i do not completely understand? [CODE] #include <iostream> #include <fstream> #include <cmath> #include <cstdlib> …

Member Avatar
Member Avatar
+0 forum 4

Hi there I've got a problem with functions, where it seems that function call return zero instead of the actual variable value, I will appreciate any help from you guys. I will post the major code parts because the program is big. I get the totallength variable value right but for t i get zero istead of 1, 2, 3, ... did I miss something here?? [CODE]using namespace std; //global variables int t; static float totallength=0; // the function void FileOutput(int a, float b) { ofstream outData("Output1.csv", std::ios::out | std::ios::app); if (outData.good()) { cout<<t<<totallength<<endl; outData << t <<","<<totallength<<endl; //writing cout<<endl; …

Member Avatar
Member Avatar
+0 forum 5

Hi there I'm trying to ofstream (output) data to an excel file (csv), and I keep getting this error "file was not declared in this scope". Can you guys help me on this? where is the error? This is the output function, [CODE]void FileOutput() { int t; static float totallength=0; ofstream outData("Output1.csv"); if (file.good()) { outData << t <<","<<totallength<<endl; //writing } file.close(); outData.close(); }[/CODE]

Member Avatar
Member Avatar
+0 forum 4

//////////////////////////////////////////////////////////////////////////////////////// // In this little project I am trying to learn to get and manipulate data from // a file input using fstream strings I also wanted to use the formating available // with the printf and related string handling methods. I have a question in regards // to the two usages that I have shown in order to get char * from std::string. // // Is there a differance between the two things I have done? // Is one safer than the other because of allocatin for size? // Does it matter? // // Does anyone have any comments …

Member Avatar
Member Avatar
+0 forum 2

Guys, I've been stuck on this for a while now. This is actually one of my assignments, and I'm really lost. This is the assignment: ------------------------------------------------------------------------------------------- You manage a .txt file containing the customer ID number and account balance of the members of a bank. Last week, a computing error caused each customer's account balance to get reversed. For example, if someone had an account balance of 132.64, this was reversed to become: 462.31. Write a program which will input the file and output a file containing the corrected account balances next to each customer ID number (a 9 digit …

Member Avatar
Member Avatar
+0 forum 32

Hello guys! I have to create a program that prints the last 10 lines of a text or all of it if the number of the lines is <=10. The input will be a file that is unknown to me. This is what I've done so far. [CODE] #include <iostream> #include <fstream> #include <string> #define LinesToPrint 10 using namespace std; int main() { ofstream outputfile; string line; int lines=0,i=0; char c; outputfile.open("newfile.txt"); while (getline(cin,line)) //I copy each line of the file to the file I created { outputfile<<line<<endl; //I put endl because I don't know if '\n' is lines++; //included …

Member Avatar
Member Avatar
+0 forum 5

i have 3 files one main.cpp, functions.cpp, and a header.h im using [CODE]#include<fstream>[/CODE] in the main.cpp and in the functions.cpp but im not sure where to define the ofstream datatype in, so that the main.cpp and the function.cpp share the same ofstream variable. where do i put this "ofstream outf;" between the 3 files main.cpp [CODE]#include <fstream> #include "cs.h" using namespace std; ofstream outf; int main () { outf.open("Results.txt"); /*for (int i = 0;i < 500;i++ ) { int random = rand() %8; switch (random) { case 0 : reader1(); break; case 1 : reader2(); break; case 2 : reader3(); …

Member Avatar
Member Avatar
+0 forum 5

Hey, Does anyone know how to cpy from one file and redirect to another. I know I have to use the ofstream but I'm stuck. [CODE] while (!in1.eof()) // while not end of input file { in1.getline(current, 100); // read a character from input file out1.in }[/CODE] I'm stuck. I want to use a while loop to get the line then place it into another file until I've reached end of file. There is a reason why I'm using getline(), I need the Carriage return (0D) to be present. Some file only have CRLF(0A) and I know getline will place …

Member Avatar
Member Avatar
+0 forum 13

[CODE]double speedTest() { double dInitialTime = clock(), dFinalTime = 0; double dRepititions = 99999999; double dResult; cout << "\nCalculating...\n\n"; for(int nCounter = 0; nCounter <= dRepititions; nCounter++) { pow(dRepititions, pow(dRepititions, dRepititions)); } dFinalTime = clock(); dResult = dFinalTime - dInitialTime; ofstream outf("Results.dat", ios::app); outf << dResult << endl; return dResult; }[/CODE] I created this function (as part of a larger program) a while back, but when I upgraded to Xcode 4, seemingly ofstream does not work anymore. While the code compiles, noting is written to the file when this function is called. I've looked over it, but can't seem to …

Member Avatar
Member Avatar
+0 forum 15

I'm trying to save everything to a .txt file with a new line after every string, but I can't get it to work for me. I tried using [ICODE]<<endl;[/ICODE] and [ICODE]"\n"[/ICODE] but it all gets written in a single line. So if I enter "John" for [ICODE]first[/ICODE], "Smith" for [ICODE]last[/ICODE], and "Casablanca" for [ICODE]film[/ICODE], I get "JohnSmithCasablanca". I've gone through all my notes and my textbook. Am I doing something wrong? [CODE]#include <iostream> #include <string> #include <fstream> using namespace std; int main() { string first, last, film; cout<<"Enter your first name and last name, then film(one word). "; cin>>first; cin>>last; …

Member Avatar
Member Avatar
+0 forum 4

Hello guys! I'm having a rather pesky problem working with fstream. I could write, for example: [CODE]fstream file("text.txt");[/CODE] then file should be open in read/write mode. But then if I do the following: [CODE]file << "Some text"; file >> string_var;[/CODE] or in the opposite order, it will only perform the first operation. I've even tried with seekp() but it still happens, even if I do: [CODE]file.open("text.txt",ios::out); file << "A word!"; file.close(); file.open("text.txt",ios::in); file >> aString;[/CODE] The only way i've found to fix this is creating two separate fstreams but it seems not right... Also, if I try to open different …

Member Avatar
Member Avatar
+0 forum 1

Hi I am trying to store an array into a file. I have tried a few ways but I end up with an extra line at the end of the file. The only way I can do this without the line is by writing [code] ofstream myfile("Stock.txt"); if (myfile.is_open()) { myfile << number[0] <<endl; myfile << number[1] <<endl; myfile << number[2] <<endl; myfile << number[3] <<endl; myfile << number[4] <<endl; myfile << number[5] <<endl; myfile << number[6] <<endl; myfile << number[7] <<endl; myfile << number[8] <<endl; myfile << number[9]; } //if this isnt possible it outputs this else cout << …

Member Avatar
Member Avatar
+0 forum 12

Hi, I'm having trouble displaying the numbers in the file on the screen. The numbers are in the .data files but i cant get them to display. [CODE]//****************************************************************************** // Exercise: Homework 07 Arrays and pointers // This program reads two sets of integers from the text files A.data and B.data. // The program finds the intersection of the two sets (which is the // set of numbers contained in both sets). The intersecting // values are displayed and saved in C.data with the same file format // tha A.data and B.data, is that to say, one interger in each line …

Member Avatar
Member Avatar
+0 forum 9

Hi there, im new to the site and am hoping someone out there can help me with my question, im pretty newbie in the world of c++ and programming in general. Im making a program with a menu, you can choose between entering a number yourself to be tested as a palindrome, or the second option opens a menu where you can enter a filename, amount of numbers, and then it generates the specified number of 5 digit random numbers into the specified file, then reads them back but in the process of reading them back it sorts them palindromes …

Member Avatar
Member Avatar
+0 forum 2

Hello. I have some problems. 1.With for cycle. This is my code: [CODE]#include <iostream> #include <fstream> int main () { int i; int i2; char sentence [200]; char repeat; ofstream file ("file.txt", ios::app); for (i=0; i<1;) { cout << "Enter sentence (max. 200 symbols)\n"; cin >> sentence; if (!file) { cout << "File opening error\n"; return 1; } file << sentence; for (i2=0; i2<1;) { cout << "Do you want to write another sentence? (y/n)\n"; cin >> repeat; if (repeat == 't' || repeat == 'n') break; else cout << "Wrong symbol\n"; } if (repeat == 'n') break; } file.close(); …

Member Avatar
Member Avatar
+0 forum 8

why doesn't this work? [CODE]int main() { fstream file; char output[100], pasword[100]; int a; const string x("Yes"); const string y("No"); string z; char b; cout << "\nEnter correct password to continue: \n" << endl; cin.getline(output, 15); if(output == "file.txt") { cout << "Change password? Type Yes or No. \n" << endl; cin >> z; if(z == x) { cout << "\nEnter current password: \n" << endl; cin >> b; if(b == "file.txt") { cout << "\nEnter new password:\n" << endl; file.open("file.txt"); file >> output; file.close(); cout << "\nVerify new password:\n" << endl; file.open("file.txt"); file >> pasword; file.close(); if(output == pasword) …

Member Avatar
Member Avatar
+0 forum 7

Hello! I'm new to c++ but I have a problem and desparate for help. I'm trying to store string and integers into a file, but only strings get stored and integers dont. I tried casting,didnt work. Not to convolute you with my code, I came up with this small program to show. Any help is greatly appreciated [code=c] int main (int argc, char * const argv[]) { ofstream outfile ("test.txt"); outfile << "hello" << endl; outfile << 44 << endl; outfile.close(); return 0; } [/code]

Member Avatar
Member Avatar
+0 forum 1

Howdy, those of you who remember me may be happy to know my 2D game engine was finished and I'm now working on a 3D engine. My engine has been going rather spiffingly well, after some battles with DirectX and OpenGL i managed to get them working side-by-side. Wrote a working wavefront OBJ model loader (i'll happily hand that out sometime) and generally was having a great time.. Now here comes the problem. Pretty-much all my code so far is cross platform, the parts that aren't have alternate versions for platforms (window management, etc). I'm about to impliment my Physics …

Member Avatar
Member Avatar
+0 forum 5

The End.