Reading data from a file to list C++ - c++

I am trying to read data from a file to a list for my program but I cannot get it to work, the data.txt would look like:
Pizza
Donut
Macaroni and cheese
And I want these items separated by new lines to each be a member of the list, I've provided code for how I attempted to solve it in C with classes below.
C With Classes
#include <iostream>
#include <list>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fstream>
#include <filesystem>
int main() {
std::ifstream myfile ("test.txt");
int i = 1;
std::list<std::string> threatList;
if (myfile.is_open()){
for(std::string line; getline(myfile, line);)
{
threatList.push_back(line);
}
myfile.close();
for (i in threatList;) {
// Check if any of the threatList iteams are real directorys and deletes them then tells the user
}
}
}
I expected to have a mutable list that I could iterate through for example if I want to print each item in the list but also allow people who cant code to add to the list by typing in the text file.

Related

Searching a vector for a string, and then find the position of the string in c++

I am trying to erase a string from a text file. To do this, I want to read the file into a vector, then I want to search for the position of this string, so I can use vector::erase to remove it. After the string has been erased from the vector, I can write the vector into a new file.
So far, I have made all of that, but finding the position of the string. I've found all sorts of solutions using < algorithm > 's std::find, but those answers were trying to check if this string exists, not its position.
Here is an example of how the text file is set up. With a string, followed by an integer, followed by .txt without spaces. Each string is on a newline.
file123.txt
Bob56.txt'
Foo8854.txt
In this case, the vector would be "file123.txt", "bob56.txt", "Foo8854.txt".
This is the code I have made already:
std::vector<std::string> FileInVector;
std::string str;
int StringPosition;
std::fstream FileNames;
FileNames.open("FileName Permanent Storage.txt");
while (std::getline(FileNames, str)) {
if(str.size() > 0) {
FileInVector.push_back(str); // Reads file, and this puts values into the vector
}
}
//This is where it would find the position of the string: "bob56.txt" as an example
FileInVector.erase(StringPosition); // Removes the string from the vector
remove("FileName Permanent Storage.txt"); // Deletes old file
std::ofstream outFile("FileName Permanent Storage.txt"); // Creates new file
for (const auto &e : FileInVector) outFile << e << "\n"; // Writes vector without string into the new file
Below is the working example. There is no need to store the string into a vector or search for the position of the string inside the vector because we can directly check if the read line is equal to the string to be searched for, as shown.
main.cpp
#include <iostream>
#include <fstream>
int main()
{
std::string line, stringtobeSearched = "Foo8854.txt";
std::ifstream inFile("input.txt");
std::ofstream outFile("output.txt");
if(inFile)
{
while(getline(inFile, line, '\n'))
{
std::cout<<line<<std::endl;
//if the line read is not same as string searched for then write it into the output.txt file
if(line != stringtobeSearched)
{
outFile << line << "\n";
}
//if the line read is same as string searched for then don't write it into the output.txt file
else
{
std::cout<<"string found "<<std::endl;//just printing it on screen
}
}
}
else
{
std::cout<<"file could not be read"<<std::endl;
}
inFile.close();
outFile.close();
return 0;
}
input.txt
file123.txt
Bob56.txt'
Foo8854.txt
file113.txt
Bob56.txt'
Foo8854.txt
file223.txt
Bob96.txt'
Foo8814.txt
output.txt
file123.txt
Bob56.txt'
file113.txt
Bob56.txt'
file223.txt
Bob96.txt'
Foo8814.txt
std::find returns an iterator to the found element and std::vector::erase accepts an iterator too. std::distance can be used to compute the index if needed.
Small example:
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
void print(const auto& vec){
for(const auto& e:vec){
std::cout<<e<<' ';
}
std::cout<<'\n';
}
int main(){
std::vector<std::string> vec{"a","b","c","d"};
auto it = std::find(vec.begin(),vec.end(),"c");
if(it!=vec.end())//If found
{
std::cout<<"Index "<<std::distance(vec.begin(),it)<<'\n';
vec.erase(it,it+1);
print(vec);
}
}
Output:
Index 2
a b d
That said, there is simple O(1) memory ( in terms of loaded lines) solution: read the lines and immediately write back only those that do not match the string.
#include <filesystem>
#include <iostream>
#include <fstream>
#include <map>
#include <cmath>
#include <chrono>
#include <algorithm>
#include <vector>
#include <execution>
#include <thread>
#include <condition_variable>
#include <mutex>
#include <string>
#include <atomic>
int main(int argc, char *argv[])
{
std::vector<std::string> b{"uyv","uky","u6t"};
std::vector<std::string> cb{"uyv"};
auto heil = std::search(b.begin(), b.end(), cb.begin(), cb.end());
b.erase(heil);
for (auto c : b)
std::cout << c << std::endl;
}

C++ Parsing a CSV file into vector of vectors: Loosing string 1st character

I am reading a CSV file into vector of string vectors. I have written code below.
#include<iostream>
#include<fstream>
#include<string>
#include <vector>
#include <fstream>
#include <cmath>
#include <sstream>
using namespace std;
int main()
{
ifstream mesh;
mesh.open("mesh_reference.csv");
vector<vector<string> > point_coordinates;
string line, word;
while (getline(mesh,line))
{
stringstream ss(line);
vector<string> row;
while (getline(ss, word, ','))
{
row.push_back(word);
}
point_coordinates.push_back(row);
}
for(int i=0; i<point_coordinates.size(); i++)
{
for(int j=0; j<3; j++)
cout<<point_coordinates[i][j]<<" ";
cout<<endl;
}
return 0;
}
When I print out the vector of vectors, I see that I am loosing the first character of Element at 0 position in the vector row. Basically, point_coordinates[0][0] is displaying 0.0001 while the string is supposed to be -0.0001. I am not able to understand the reason for the same. Kindly help.
A typical output line is
.0131 -0.019430324 0.051801
Whereas the CSV data is
0.0131,-0.019430324,0.051801
SAMPLE CSV DATA FROM FILE
NODES__X,NODES__Y,NODES__Z
0.0131,-0.019430324,0.051801
0.0131,-0.019430324,0.06699588
0.0131,-0.018630324,0.06699588
0.0131,-0.018630324,0.051801
0.0131,-0.017630324,0.050801
0.0131,-0.017630324,0.050001
0.0149,-0.017630324,0.050001
0.0149,-0.019430324,0.051801
Although the problem is already solved, I would like to show you a solution using some modern C++ algorithms and eliminating minor issues.
Do not use using namespace std;. You should not do this
Ne need for a separate file.open. The std::ifstream constructor will open the file for you. And the destructor will close it
Check if the file could be opened. The ifstreams ! operator is overloaded. So you can do a boolean check
Do not use int in for loops where you compare against .size(). Use ````size_t instead
Always initialize all variables, even if there is an assignement in the next line
For tokenizing you should use std::sregex_token_iterator. It has exactly been designed for this purpose
In modern C++ you are encouraged to use algorithms
Please see an improved version of your code below:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <regex>
const std::regex comma(",");
int main()
{
// Open source file.
std::ifstream mesh("r:\\mesh_reference.csv");
// Here we will store the result
std::vector<std::vector<std::string>> point_coordinates;
// We want to read all lines of the file
std::string line{};
while (mesh && getline(mesh, line)) {
// Tokenize the line and store result in vector. Use range constructor of std::vector
std::vector<std::string> row{ std::sregex_token_iterator(line.begin(),line.end(),comma,-1), std::sregex_token_iterator() };
point_coordinates.push_back(row);
}
// Print result. Go through all lines and then copy line elements to std::cout
std::for_each(point_coordinates.begin(), point_coordinates.end(), [](std::vector<std::string> & vs) {
std::copy(vs.begin(), vs.end(), std::ostream_iterator<std::string>(std::cout, " ")); std::cout << "\n"; });
return 0;
}
Please consider, if you may want to use such an approach in the future

Getting Information from input file C++

I'm pretty new to coding so I'm not entirely sure if I'm doing file extraction correct. I'm getting lldb as my output for this code. Instead of prompting the user with the words in the hangman.dat file.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream sourceFile;
sourceFile.open("hangman.dat");
if (sourceFile.fail())
{
cout<<"File didn't open" ;
}
else
{
string words;
sourceFile >> words;
while(sourceFile>>words)
{
cout<<words<<endl;
}
}
}
The file hangman.dat contains the following information:
Fall
leaves
Thanksgiving
pumpkins
turkey
Halloween

Getline only printing out last line when called multiple times

So this is a fairly simple example of a program where I'm trying to output the first two lines of an input text file. The ifstream should be a global variable, and the testGetFile() function is necessary (I have not done the actual text processing needed in this code.) I'm trying to figure out why this is cout-ing only the SECOND line of the input file. Any help will be appreciated!
Thanks in advance!
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
ifstream input;
string testGetFile(){
string result;
getline(input,result);
return result;
}
int main(){
input.open("testInput.txt");
cout<< testGetFile();
cout<< testGetFile();
return 0;
}

Most Compact Way to Count Number of Lines in a File in C++

What's the most compact way to compute the number of lines of a file?
I need this information to create/initialize a matrix data structure.
Later I have to go through the file again and store the information inside a matrix.
Update: Based on Dave Gamble's. But why this doesn't compile?
Note that the file could be very large. So I try to avoid using container
to save memory.
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
int main ( int arg_count, char *arg_vec[] ) {
if (arg_count !=2 ) {
cerr << "expected one argument" << endl;
return EXIT_FAILURE;
}
string line;
ifstream myfile (arg_vec[1]);
FILE *f=fopen(myfile,"rb");
int c=0,b;
while ((b=fgetc(f))!=EOF) c+=(b==10)?1:0;
fseek(f,0,SEEK_SET);
return 0;
}
I think this might do it...
std::ifstream file(f);
int n = std::count(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), '\n') + 1;
If the reason you need to "go back again" is because you cannot continue without the size, try re-ordering your setup.
That is, read through the file, storing each line in a std::vector<string> or something. Then you have the size, along with the lines in the file:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
int main(void)
{
std::fstream file("main.cpp");
std::vector<std::string> fileData;
// read in each line
std::string dummy;
while (getline(file, dummy))
{
fileData.push_back(dummy);
}
// and size is available, along with the file
// being in memory (faster than hard drive)
size_t fileLines = fileData.size();
std::cout << "Number of lines: " << fileLines << std::endl;
}
Here is a solution without the container:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
int main(void)
{
std::fstream file("main.cpp");
size_t fileLines = 0;
// read in each line
std::string dummy;
while (getline(file, dummy))
{
++fileLines;
}
std::cout << "Number of lines: " << fileLines << std::endl;
}
Though I doubt that's the most efficient way. The benefit of this method was the ability to store the lines in memory as you went.
FILE *f=fopen(filename,"rb");
int c=0,b;while ((b=fgetc(f))!=EOF) c+=(b==10)?1:0;fseek(f,0,SEEK_SET);
Answer in c.
That kind of compact?
#include <stdlib.h>
int main(void) { system("wc -l plainfile.txt"); }
Count the number of instances of '\n'. This works for *nix (\n) and DOS/Windows (\r\n) line endings, but not for old-skool Mac (System 9 or maybe before that), which used just \r. I've never seen a case come up with just \r as line endings, so I wouldn't worry about it unless you know it's going to be an issue.
Edit: If your input is not ASCII, then you could run into encoding problems as well. What's your input look like?