Reading from .csv [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
So I am reading from the .csv as showed as the result is shown in the image url. When I'm cout-ing everything what's in the file it prints additional " " between the every read cell. So I'm wondering why are these " " cout-ed? And most importantly is there a way to get rid of them?
void Evidenca::IskanjeIstiVnos(vector<Evidenca>& evidenc, vector<Evidenca>& kopir, fstream &Datoteka){
string vrstica;
int StVrstic=0;
int Stej;
string TEMPsemester;
string TEMPletnik;
string TEMPects;
string TEMPocIzpita;
while(!Datoteka.eof()){
getline(Datoteka,vrstica,'\n');
Datoteka >> ws;
StVrstic++;
}
Datoteka.clear();
Datoteka.seekg(0, ios::beg);
cout << "Stevilo predmetov zapisanih v datoteki je: " << StVrstic<<endl; //How many lines are in file
for(int a=0;a<StVrstic;a++){
getline(Datoteka,ImePredmeta,';');
getline(Datoteka,ProfesorImPr,';');
getline(Datoteka,TEMPsemester,';');
getline(Datoteka,TEMPletnik,';');
getline(Datoteka,TEMPects,';');
getline(Datoteka,datumIzpita,';');
getline(Datoteka,TEMPocIzpita,'\n');
cout << ImePredmeta << " " << ProfesorImPr << " "<< TEMPsemester << " " << TEMPletnik << " " << TEMPects << " " << datumIzpita << " " << TEMPocIzpita << endl;
}

getline doesn't make up characters. Those characters are read from your CSV file : you can see them if you open it with a text editor (emacs, vi, notepad..), they are automatically hidden in Excel-like software.

Related

Excel to C++? Won't Work, EVERY online youtube Tut works for them, they don't work for moi [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 2 years ago.
Improve this question
read Excel w/ C++ (microsoft vis studio)
See tutorial:
https://www.youtube.com/watch?v=EjJY7yA5SWw
Here's my code:
*ifstream FILE("USE THIS FILE.xls");
//
string date; string companyTicker; string companyName; int year1; int til; int month; int year2; int asdf;
while(FILE>> date >> companyTicker >> companyName >> year1 >> til >> month >> year2 >> asdf)
{
std::cout << date << ", " << companyTicker <<
", " << companyName << ", " << year1 << ", " << til << ", " << month << ", " << year2 << ", " << asdf << endl;
}*
I've also gone on another tutorial where istream::getline is asked, of course this will cause an error "no instance of overloaded function". nothing online has been able to diagnose that issue.
Thanks all!
The tutorial you linked to shows a text file with fields separated by whitespace , which is how stream input in C++ is delimited. An XLS file is in a proprietary binary format, and cannot be directly read (without a library specifically designed for reading XLS files. Save your data as a tab-delimited file if you want to easily read it from C++.

How can i keep the value of a variable even after the programm ends? [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 3 years ago.
Improve this question
I am trying to use a variable value even after the programm runs out....I want to store more accounts using this variable, but everytime i press F9 it lose the value, and start again from 0; I want after the first F9 to increase the value by 1, and the notpad I create with accounts i want to has every account...I am not sure I make myself understandable but maybe someone can help me.
#André Cascais and #ds4940 are right. Include the fstream library in your program and use ifstream to read a file and ofstream to write to a file. You can also use fstream to do both reading and writing.
Example with text file:
#include <iostream>
#include <fstream>
int main()
{
std::ifstream in_file{"test.txt"};
// check if the file opened
if (!in_file)
{
std::cerr << "Unable to open test.txt\n";
return 1;
}
std::string str;
int x{};
double y{};
// read the values from file
in_file >> str >> x >> y;
in_file.close(); // close the file
// truncate test.txt
std::ofstream out_file{"test.txt"};
if (!out_file)
{
std::cerr << "Unable to open test.txt\n";
return 1;
}
// display data
std::cout << str << " " << x << " " << y << std::endl;
// write new data
out_file << str << " " << x + 1 << " " << y + 1 << std::endl;
out_file.close(); // close the file
return 0;
}
This example assumes you already have the file when the program runs. If you want to read by line use the getline method. Hope this helps!

How to copy array to file in c++ [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 5 years ago.
Improve this question
I read array from the text file and I want to copy this array's elements to another text file
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int ARRAY_SIZE = 5;
int numbers[ ARRAY_SIZE];
int count = 0;
cout << "1. before opening file\n";
ifstream inputFile;
inputFile.open("test.txt");
if (!inputFile)
{
cout << "error opening input file\n";
return 1;
}
cout << "2. after opening file\n";
cout << "3. before reading file, count = " << count << '\n';
while (count < ARRAY_SIZE && inputFile >> numbers [ count])
count++;
inputFile.close();
cout << "4. after reading file, count = " << count << '\n';
cout<< "The numbers are : ";
for (int i = 0; i < count; i++)
cout << numbers[i] << " ";
cout<< endl;
cout << "5. Program ending" << endl;
return 0;
}
I added this code but it doesn't work. How can I copy this array's elements to destination.txt file?
ofstream fstreamFile("destination.txt");
copy(
numbers,
numbers + sizeof(numbers),
ostream_iterator<int>(fstreamFile)
);
my elements are 10,20,30,40 but in the destination.txt file, output is "10203040160641613632767-1973944304-...."
The problem is that you use sizeof for the end "iterator" of the array.
The sizeof operator returns the size in bytes, not in array elements. That means you will go way out of bounds beyond the end of the array.
I suggest you change to use the standard std::begin and std::end helper functions to get the "iterators" for the array:
std::copy(std::begin(numbers), std::end(numbers), ...);
For proper arrays (but not for pointers, and remember that arrays decays to pointers very easily) those functions will do the right thing.

Having problems declaring a string [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 7 years ago.
Improve this question
So, I started using C++ today, and wanted to make a small, text-based adventure, but that didn't turn out all that well.
Here's my code:
string answer;
string name;
string charName;
std::cout << "<Ominous voice> Hi there, what's your name?" << std::endl;
std::cout << "Enter your name:" << std::endl;
getline(cin, name);
std::cout << "<Ominous voice> " << name << "? That's an... interesting name." << std::endl;
std::cin.ignore();
std::cout << "<Ominous voice> Before we start off... You need to learn the basics, so let's break the fourth wall shall we?" << std::endl;
std::cin.ignore();
std::cout << "<Ominous voice> I'm here to guide you on this wonderfull adventure... for just $ 9.99." << std::endl;
std::cin.ignore();
std::cout << "<Ominous voice> You can name me everything you want, since I'm a fragment of your imagination anyway... So, what about it?" << std::endl;
std::cout << "Enter a name:" << std::endl;
getline(cin, charName);
strcpy (str1,"<");
strcpy (str2,">");
strcat (str1,charName, 1);
strcat (charName,str2, charName.size();
std::cout << charName << " Well then, it seems I'm now called '" << charName << "' not sure if I like that." << std::endl;
std::cin.ignore();
std::cout << "This line does not show up" << std::endl;
Now I have 2 problems:
I get these messages when I start the program:
/home/ubuntu/workspace/hello-cpp-world.cc: In function ‘int main()’:
/home/ubuntu/workspace/hello-cpp-world.cc:23:13: error: ‘str1’ was not declared in this scope
strcpy (str1,"<");
^
/home/ubuntu/workspace/hello-cpp-world.cc:24:13: error: ‘str2’ was not declared in this scope
strcpy (str2,">");
^
/home/ubuntu/workspace/hello-cpp-world.cc:26:43: error: expected ‘)’ before ‘;’ token
strcat (charName,str2, charName.size();
^
That last line, saying "This line does not show up", actually doesn't show up.
I know I'm basic and this is probably not the most efficient way to do this, but I'm a beginner.
You have to declare string str1 before using it. Decraling means
std::string str1;
In C++ the definition is e.g.
str1 = "heureka";
And the declaration has always be in front of the definition.
But you can do both in one line:
std::string str1 = "heureka";
or
std::string str1("heureka");
If you want to add "<" in front and ">" behind the characters name. You can do it as follows:
charName = "<" + charName + ">";
There is no need to create new strings for it. There is simply a + operator for strings and it is easier to use than strcpy, strcat or so.
The issue is that you haven't declared the strings str1 and str2.

Ofstream returning garbage. Cout works... Why doesn't ofstream?

So I'm trying to design a program that inputs a file and then reads through the lines and takes each line and outputs info about it to a new file.
I have it all down... except! All my .txt files are filled with garbage instead of what they should be filled with.
I can not figure it out. If I cout the string I'm feeding into the ofstream, the right stuff prints on the screen.
fstream lineOutFile;
string newFileName;
stringstream linefile;
linefile << lineCt;
linefile >> newFileName;
newFileName += ".txt";
lineOutFile.open(newFileName.c_str());
if(!lineOutFile)
{
cerr << "Can't open output file.\n";
}
stringstream iss;
iss << "The corrected 5' x 3' complement of line " << lineCt <<
" is as follows - \n\n" << finalSeq << "\n\n\n\n" <<
"This line of DNA sequence is made up of " << cgContent <<
" C and G neucleotides.\n\n" << "It contains " << polyTCount <<
" Poly-T strings of more than 4 consecutive neucleotides. They are as follows. - \n" <<
polyTString << "\n\n There are " << cpgCount <<
" CpG sites. The locations are as follows - \n" << cpgString;
string storage;
storage = iss.str();
cout << storage;
lineOutFile << storage;
lineOutFile.close();
lineCt++;
}
I'm getting "⁥潣牲捥整⁤✵砠㌠‧" << This sort of craziness in my .txt files.
I get the right thing when I cout out the same sting immediately before!
Why are my .txt files garbage?
Why are you using fstream instead of ofstream?
The default for fstream is to open an existing file. The default for ofstream is to start with an empty file. Whatever is already in the file is probably causing your editor to interpret the data with the wrong encoding.
Try running
LANG=C your_program
And turn off encoding recognition in your editor. Even better - view your txt files with cat program.