getline c++ need assistance [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm trying to read individual lines to different variables and I'm having trouble having it read anything past the first line
ifstream inputFileStream;
inputFileStream.open( fileName );
if (inStream.good() && inStream.open())
{
string empNum;
string name;
string streetAddress;
getline(inStream, empNum);
getline(inStream, name);
getline(inStream, streetAddress);
cout <<empNum << endl;
cout <<name << endl;
cout <<streetAddress << endl;
}
Also the txt file looks like this:
12
Bob
123 Main
555-555

This code works for me. copy & paste & run (without changes...)
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
ifstream inputFileStream("text.txt");
if (inputFileStream.good() )
{
string empNum;
string name;
string streetAddress;
getline(inputFileStream, empNum);
getline(inputFileStream, name);
getline(inputFileStream, streetAddress);
cout <<empNum << endl;
cout <<name << endl;
cout <<streetAddress << endl;
}
return 0;
}

Related

the output of the code in the text file is weird characters and it should be names and numbers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
class admin {
public:
string ID;
string any;
string Name;
string librarian;
void getdata();
void showdata();
void adddata();
void displaydata();
};
void admin::getdata() {
ofstream file_obj;
file_obj.open("Students.txt", ios::app);
admin obj;
string id;
string name;
string lib;
cout << "Enter Student Details......\n\n";
cout << "Enter ID No. : ";
cin >> ID;
cout << "Enter Full Name : ";
cin >> Name;
cout << "Enter librarian name. : ";
cin >> librarian;
obj.ID = id;
obj.Name = name;
obj.librarian = lib;
file_obj.write((char*)&obj, sizeof(obj));
}
void admin::showdata() {
ifstream file_obj;
file_obj.open("Students.txt", ios::in);
admin obj;
string id;
string name;
string lib;
obj.ID = id;
obj.Name = name;
obj.librarian = lib;
file_obj.read((char*)&obj, sizeof(obj));
cout << "\n\n.......Student Details......\n";
cout << "ID No. : " << ID << endl;
cout << "Full Name : " << Name << endl;
cout << endl;
cout << " librarian name. : " << librarian << endl;
}
int main() {
admin myobj;
myobj.getdata();
myobj.showdata();
}
The output of the code in the text file is weird characters and it should be names and numbers, I dont know where is the problem.
The problem is this:
file_obj.write((char*)&obj, sizeof(obj));
Your class admin contains objects that include pointers to other objects. You save the address of these pointers to your file.
file_obj.read((char*)&obj, sizeof(obj));
But when you reload the object, you make no effort to fix these pointers (the address where they are pointing could now point at something completely different).
Note: Overwriting objects like this (anything non trivial) is technically Illegal. The only valid way of creating or updating a complex object like a string is via its method on the API. Just writing over the object basically is invalid and not allowed.
string Name;
Technically the type std::string could contain anything (you have no idea). But let us assume for argument that it has three pointers in it.
These point at an area of memory that contain the string data. But when you read the object from file what is in that area of memory now. Also the actual implementation of std::string could change between platform and/or version of compiler (so it could have a different size).

C++ Change a word to another [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I want to write a tool to change the word to another one, as I give! It matches that already some day and all the time there is a problem that my word is cut off or something is wrong. then I want a better program with the use of std :: regex but I can't cope with this problem anymore ..
#include <iostream>
#include <fstream>
void display_members()
{
std::string oldWord;
std::string newWord;
std::string tmp;
std::string getcontent;
std::ifstream openfile ("test", std::ios::in | std::ios::app);
std::cin >> oldWord;
std::cin >> newWord;
// if(openfile.is_open())
// {
while(! openfile.eof())
{
getline(openfile,tmp);
while((tmp.find(oldWord)) != std::string::npos)
{
tmp.replace(tmp.find(oldWord),newWord.length(),newWord);
}
std::cout << ","<<tmp << " " << " ";
// openfile >> getcontent;
// std::cout << getcontent<< " ";
}
// }
openfile.close();
}
int main()
{
display_members();
}
Try something more like this instead:
#include <iostream>
#include <fstream>
void display_members()
{
std::string oldWord, newWord, tmp;
std::string::size_type pos;
std::cin >> oldWord;
std::cin >> newWord;
std::ifstream openfile("test");
while (getline(openfile, tmp))
{
pos = 0;
while ((pos = tmp.find(oldWord, pos)) != std::string::npos)
{
tmp.replace(pos, oldWord.length(), newWord);
pos += newWord.length();
}
std::cout << "," << tmp << " " << " ";
}
}
int main()
{
display_members();
}

C++: Cannot copy line from an input file to an output file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is a home work question, so if you are not a fan of those I understand. Here is my code:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
fstream myfile1("datafile1.txt"); //this just has a bunch of names in it
fstream myfile2("cmdfile1.txt"); //has commands like "add bobby bilbums"
ofstream outputFile("outfile1.txt"); //I want to take the "add bobby" command and copy the name into this new file.
string line;
if (myfile1.is_open() && myfile2.is_open()) //so I open both files
{
if (myfile2, line == "add"); //If myfile2 has an "add" in it
{
outputFile.is_open(); //open outputfile
outputFile << line << endl; //input the line with add in it till the end of that line.
}
}
cout << "\nPress Enter..."; // press enter and then everything closes out.
cin.ignore();
outputFile.close();
myfile2.close();
myfile1.close();
return 0;
}
Problem is, though the outputFile is always empty. It never copies any lines from cmdfile1 into the output file. Does anyone know what I am missing here?
Try something more like this instead:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream myfile1("datafile1.txt");
ifstream myfile2("cmdfile1.txt");
ofstream outputFile("outfile1.txt");
string line;
if (/*myfile1.is_open() &&*/ myfile2.is_open() && outputFile.is_open())
{
while (getline(myfile2, line))
{
if (line.compare(0, 4, "add ") == 0)
{
outputFile << line.substr(4) << endl;
}
}
}
myfile1.close();
myfile2.close();
outputFile.close();
cout << "\nPress Enter...";
cin.ignore();
return 0;
}

How do I create rows in an output file? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I currently have this code and all of the formatting is correct... I just can't seem to get it to create rows in the output text file... How would I do this? I've tried to do a for loop in the function and in the main() but it seems to not work if I do that so I am very confused right now.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void output(string flightnumber, int arrival1, int arrival2, int realarrival1, int realarrival2, char dummy1, char dummy2)
{
ofstream flight;
flight.open("flightdata.dat");
if (flight.fail())
{
cout << "Error..." << endl;
exit(1);
}
cout << "Enter the flight number: ";
cin >> flightnumber;
if (flightnumber == "end")
{
exit(1);
}
flight << flightnumber << setw(4);
cout << "Enter the scheduled/actual arrival times (hh:mm hh:mm):";
cin >> arrival1 >> dummy1 >> arrival2 >> realarrival1 >> dummy2 >> realarrival2;
flight << arrival1 << dummy1 << arrival2 << setw(4) << realarrival1 << dummy2 << realarrival2;
flight << ('\n');
}
int main()
{
string flightnumber;
int arrival1, arrival2, realarrival1, realarrival2;
char dummy1, dummy2;
output(flightnumber, arrival1, arrival2, realarrival1, realarrival2, dummy1, dummy2);
return 0;
}
You are not appending your file whenever you write it truncates and creates a new file, add appending flag and open your file as.
flight.open("flightdata.dat", ios_base::app);
You are using uninitialized variables in main. They serve no purpose in there anyway. Remove variable declarations from main and put them in output:
void output()
{
string flightnumber;
int arrival1, arrival2, realarrival1, realarrival2;
char dummy1, dummy2;
ofstream flight;
flight.open("c:\\test\\___flightdata.txt", ios::app);
...
}
int main()
{
output();
return 0;
}
You may want to add ios::app flag as pointed out in the other answer.

how to make variables out of a string in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
this is my code below, the output is numbers relating to a grade. I want to be able to make each line of the string a variable such as int grade1 and so on, so that I can display certain variables and not the whole string for example cout << grade1 << grade2 << endl;
using namespace std;
int main()
{
string line;
ifstream myfile("Data.csv"); //opening the CVS file
if (myfile.is_open())
{
while (getline(myfile, line)) //used to read each line of the file
{
string str(line); //making the file a string
char chars[] = ",abcdefghijklmnopqrstuvwxyzN;:/"; //Characters to be removed
for (unsigned int i = 0; i < strlen(chars); ++i)
{
str.erase(std::remove(str.begin(), str.end(), chars[i]), str.end());
}
str.replace(0, 9, " ");
cout << str << endl;
}
myfile.close(); //closes the file
}
else
cout << "pathway to file can't be found" << '\n'; //error message to display if file location cant be found.
cin.get();
return 0;
}
The question and especially the code is rather difficult to comprehend, but I understood that you want to create string variables for each line of the file.
A simple way is to loop through the file line-by-line, store the line into a string variable and then push that variable to a vector.
std::string line;
std::vector<std::string> lines;
while (getline(myfile, line))
{
lines.push_back(line);
}