I'm writing a code that will save the username of a user when a button (in this case button1) gets pressed. I save it to a file called info.txt, and i use fstream in order to save it. Currently I'm running into the issue that, while i can open up the file fine, I can't save the data from textBox1 because it uses the data type String ^, while fstream doesn't support writing in that type. Here is my relevant code: private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) { } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { std::ofstream settings("settings.txt"); settings >> …

Member Avatar
Member Avatar
+0 forum 2

C++: I'm using Microsoft Visual Studio 2012 and I am not sure what to do about these two errors: 2 IntelliSense: identifier "x" is undefined Error 1 error C2065: 'x' : undeclared identifier Prompt: Make program that reads 20 integers, display that array, and find min, max, sum, and avg. One method named "read from user file" which will ask the user to input a filename and will then read from 1 to 20 integers from that file, but not calculate any statistics. Second method will display the values read in, in the order they were found in the file. …

Member Avatar
Member Avatar
+0 forum 6

C++: I'm using Microsoft Visual Studio 2012 and I am really confused and am getting errors. Prompt: Make program that reads 20 integers, display that array, and find min, max, sum, and avg. One method named "read from user file" which will ask the user to input a filename and will then read from 1 to 20 integers from that file, but not calculate any statistics. Second method will display the values read in, in the order they were found in the file. Third method will calculate the statistics and store these in the appropriately named variables that are also …

Member Avatar
Member Avatar
+0 forum 8

I am using fstream to open and close a file. The code works OK, except the the file does not close when I close it the first time. I need to invoke close twice. Any idea why? And, what this is saying about my code that I do not realize?? The code: fstream cif; cif.open(INFO.c_str(), ios::in); MYgetline(cif, cifline);// header MYgetline(cif, cifline);; // pointer cif Vector cifrule(countcommas(cifline)-1); int comma=2; for (int i=0; i<cifrule.length(); i++) cifrule(i)=comma_read(comma,cifline); for (int c=0; c<cifrule.length(); c++) { Outcome(0,(cifrule(c)-1))=NewStatData(0,(cifrule(c)-1)); Outcome(1,(cifrule(c)-1))=NewStatData(1,(cifrule(c)-1)); Outcome(2,(cifrule(c)-1))=NewStatData(2,(cifrule(c)-1)); } cif.close(); if (cif) cout << "cif open\n"; else cout << "cif closed\n"; cif.close(); if (cif) cout …

Member Avatar
Member Avatar
+0 forum 4

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

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 3

I'm not understanding why i'm able to read from the file but i can't write to it. The fstream is open in both modes. Once the program finishes i haven't changed the content of the file. Thank you in advance. #include <fstream> #include <string> #include <iostream> int main() { std::string line; std::fstream m_File("/home/user/file.txt", std::ios::in | std::ios::out); if(m_File.fail()) return -1; // read file std::getline(m_File, line); while(m_File.good()) { std::getline(m_File, line); std::cout << line << std::endl; } // write to file std::string line2 = "test to write"; m_File.seekp(std::ios_base::beg); m_File << line2; m_File.close(); return 0; }

Member Avatar
Member Avatar
+0 forum 6

Hello, I have a bug in my program somewhere and I cannot understand why. The program merely prints data from a binary file. Here is the code: #include <iostream> #include <fstream> #include <vector> #include <cstdint> using namespace std; int main(int argc, char *argv[]) { if (argc!=2) { cout<<"Invalid argument count, please provide a file name."<<endl; return 0; } fstream file(argv[1],ios_base::binary|ios_base::in); vector<int32_t> data; int32_t tmp; char buf; while (!file.eof()) {//complicated i/o to ignore endianness file.read(&buf,1); tmp=buf<<24; file.read(&buf,1); tmp|=buf<<16; file.read(&buf,1); tmp|=buf<<8; file.read(&buf,1); tmp|=buf; data.push_back(tmp); } for (size_t i=0; i<data.size(); ++i) cout<<hex<<data[i]<<endl; return 0; } Most of the time it works fine. However, …

Member Avatar
Member Avatar
+0 forum 8

Hello, Looking at the standard streams provided by the stl libraries, it seems as though it is impossible to have 1 stream to use for both standard console IO and file/string IO. However, before I give up I would like to ask, is this possible: generic_stream stream; stream=cin; //now I can use stream as cin stream>>value; stream=fstream("myFile.txt"); //now stream acts as a stream to myFile... value>>stream; stream=sstream("123 test"); From what it looks like, since fstreams use input and output they derive iostream, which in turn derives istream, which in turn derives ios. However cout uses ostream instead of istream and …

Member Avatar
Member Avatar
+1 forum 7

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

I m reading data from a text file and then printing it. I am not sure what I am doing wrong but the program prints the last line that it reads twice. void bank_account::viewHistory() { ifstream in_file("bank_transaction_history.txt" , ios::in); if(!in_file) { cout<<"File error \n"; } cout<<"Event"<<setw(15)<<"Amount"<<setw(15)<<"Date"<<setw(20)<<"Current Balance"<<endl; string eve,dat; double am,bal; while(!in_file.eof()) { in_file>>eve>>am>>dat>>bal; //reading data from file cout<<eve<<setw(10)<<am<<setw(20)<<dat<<setw(10)<<bal<<endl; } in_file.close(); }

Member Avatar
Member Avatar
+0 forum 3

For my c++ class i have to write a code that will read integers in to a 2D array from a file and then, using functions/methods, determine if the array is a magic square. My problem is that when i build or compile i get "error C2664: 'DataIn' : cannot convert parameter 1 from 'std::ifstream' to 'std::fstream &'" Here is the code: #include <iostream> #include <fstream> using namespace std; void PrintMatrix( int x[][20], int size) { for(int i = 0; i < size; ++i) { for(int j = 0; j < size; ++j) { cout << x[i][j] << "\t"; } …

Member Avatar
Member Avatar
+0 forum 4

I'm tring to make a programe which takes the string from user and save in file(data_file). But I also want to take the .txt file from window and read the file and save in my programe file(data_file). user just give the address of .txt file or any other method in c++ programe

Member Avatar
Member Avatar
+0 forum 1

how I can modify the ofile object of fstream library, and the remaining data remain save as it was. e.g ofile: hello world name class this is the data ofile have and in modification i want to replace the name with "roll number". after modification ofile became ofile: hello world roll number class can anybody help me?

Member Avatar
Member Avatar
+0 forum 2

Hi What is the difference between using the **fstream** library to read a file....and using something like a procedure below using the FILE* object? There seems to be two ways to read a file? Thanks /* FEOF example */ #include <stdio.h> int main() { FILE * pFile; char buffer [100]; pFile = fopen ("myfile.txt" , "r"); if (pFile == NULL) perror ("Error opening file"); else { while ( ! feof (pFile) ) { if ( fgets (buffer , 100 , pFile) == NULL ) break; fputs (buffer , stdout); } fclose (pFile); } return 0; }

Member Avatar
Member Avatar
+0 forum 2

Hello, I'm having a little trouble with a some code that I'm writing for homework. Basically, I'm taking two files that contain numbers and merging the contents into another file. The files do not have the same amount of numbers in them (one might contain 7 while the other contains 10) and the numbers are sorted from small to large in each file al ready. My problem is that not all the numbers are being written to the output file. Here is the code below: #include <iostream> #include <fstream> using namespace std; void MergeFiles(ifstream&, ifstream&, ofstream&); int main() { ifstream …

Member Avatar
Member Avatar
+0 forum 3

I have been getting used to c++ as I have been trying various scenarious on programs and exand on them with new concepts. This is my first try at exporting data from command line to a text file and the program runs fine with all the calculations. The real issue is that none of the outputs are exported to the text file. It is probably some simple oversight and want to say thanks in advance for helping me. Note- This program is supposed to take the inputs of a teacher and export the individual grade w/ letter the number of …

Member Avatar
Member Avatar
+0 forum 7

Hi, could anybody please tell me what i am doing wrong in this program. The goal is to read the student name and grade from a text file and print it on the screen. This is what i have so far. by the way this is for a school project that is due this coming Monday. The attached file is the input source. [CODE]#include<iostream> #include<fstream> #include<string> using namespace std; //void calculateAverage(); //int calculateGrade(); //void updateFrequency(); int main() { const int size=10; int debug=size; int test1[size], test2[size], test3[size], test4[size], test5[size]; ifstream infile; for(int i=0;i<size;i++) { test1[i]=0; test2[i]=0; test3[i]=0; test4[i]=0; test5[i]=0; } …

Member Avatar
Member Avatar
+0 forum 4

I am trying to add a comma to the end of each line ('\n') of a text file til the end of the file is reached. I am opening the file in append mode but am unsure as to how to go about identifying when the end of line is reached and how to add the comma (perhaps replace the newline char with the comma). Any suggestions?

Member Avatar
Member Avatar
+0 forum 3

I need help running my program this is what it asks me for "Write a program that asks the user for the name of a text file. The program should display the first 10 lines of the file on the screen. If the file has fewer than 10 lines, the entire file should be displayed along with a message indicating the entire file has been displayed" I think i have coded the program correctly but when I try to run it, it doesnt run...please help... this is what i have so far... Thanks in advance... #include <iostream> #include <fstream> using …

Member Avatar
Member Avatar
+0 forum 6

When im using fstream.h its giving an error with using namespace std, how ever when i use simple fstream without .h , it is not giving error but not reading the file, How can i use fstream object using fstream.h header file along with using namespace std? Your urgent response will be highly appreciated, #include <iostream> using namespace std; #include<fstream.h> //giving error fstream file; char a[20]; int main() { file.open("doc.txt",ios::in); while (file>>a) { cout<<a<<endl; } file.close(); }

Member Avatar
Member Avatar
+0 forum 6

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've been reading quite a few threads about creating new files if one doesn't exist...but I need it specifically for windows, in the current directory we are in. I've seen ` ifstream file; file.open("TehLeetFile"); if(!file.is_open()) cout<<"Tehleetness is busted!"; else cout<<"it work!!!!11!!!11!one!"; ` That's just checking to see if the file is there. Later on I will need to read the file...but not yet.

Member Avatar
Member Avatar
+0 forum 2

Hello! I'm new here, so correct me if this is the wrong location... but I'm trying to make a program for the Minecraft server I'm on, and it requires that if someone wants to make a map that is used on the server, that there's a .xml file that shows all the information, like items the person spawns with, etc. I wrote the majority of the program recently, so I can remember almost all of what happens when :P Here's my program: #include <iostream> #include <string> #include <stdlib.h> #include <fstream> using namespace std; int screen() { cout << ".-------------------------------." << …

Member Avatar
Member Avatar
+0 forum 7

> Write an EFFICIENT program that reads in the name of a file containing positive integers that are less than 1,000,000. The program prints out the integers in the file, in the order that they appear, eliminating duplicates. So if the file contained 8 3 1 9 2 8 15 9 8 4, the program would print 8 3 1 9 22 15 4. Here I am not even sure how to approach. Do we read in the file name as an int array, and then eliminate duplicates and save the result in that same file? Any suggestions would be …

Member Avatar
Member Avatar
+0 forum 14

Hi - I'm trying to make a databse with the produkts from McDonald, but i can't figure out how. It is supposed to both read and write. Here's what i've made so far... Hope U understand: #include <iostream> #include <fstream> using namespace std; struct Menu { char navn[50]; int pris; char ingredienser[50]; float kalorier; float fedt; float kulhydrater; float protein; char allergi[50]; float forbranding; }; char menu[25]; char valg; int vare=0; char valg2; int check = 1000; int main() { cout << "Vil du indtaste nye data?(j/n) \n"; cin>>valg; if (valg=='j') { do { cout << "Indtast navn:\n"; cin>>menu; check …

Member Avatar
+0 forum 0

Here is the exercise: > fileWord > Write the definition of a function named fileWord that receives a string argument that is the name of a file containing words. > > The first time the function is called, it returns the first word of the file whose name it was passed. The second time the function is called, it returns the second, word of the file whose name it was passed (which could be a different file than was passed the first time). The third time, it returns the third word in the file whose name it was just passed, …

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

The End.