I am doing my work but suddenly while reading data from file, i suddenly faced text in other form i also carried a small test to check whether is it my code fault or Visual Studio 2012 gone mad. I also debug but values in "line[]" coming correct. But i found small test is still giving me same thing:
here is my small code test:
file: input.txt
{
value := (rate * dollar) + (rate1 * dollar1);
totalvalue := value / 2;
t1 = ivariable > 10 && ivariable < 100
{
value := (rate * dollar) + (rate1 * dollar1);
totalvalue := value / 2;
}
}
code:
#include <sstream>
#include <fstream>
using namespace std;
void main(){
const int si = 1500;
string line[si];
ifstream infile;
infile.open("input.txt");
cout<<"Reading"<<endl;
// infile>>data;
// cout<<data<<endl;
int a=0;
int size=0;
// string line[1500];
// for (int i=0;i<10;i++){
while(!infile.eof()){
getline(infile,line[a],'\n');
cout<<line[a]<<endl;
a++;
}
}
and the output:
Any idea please share......
Looks like you have copied down some text from pdf either from Adobe or Nitro and pasted in notepad as a .txt. Never do such thing always try to either by write your self or use Notepad editor. I also faced such thing while doing compiler. My teacher gave me inputs in pdf file and i copied. Sorry for late reply i was reading Light comments and me a laugh that he such you.... Good Luck!
Your input.txt is encoded in Unicode, but your console is not set to render Unicode.
Save your file as plain ASCII.
Related
I've looked up similar posts here, but none seem to be doing the job for my question. I'm basically trying take a sequence of words in a .txt file and put each word in a vector, and printing each value afterwards. For example, we have I love racing cars in array.txt, and I want my vector to have "I" at position 0, "love" at 1 and so on. Unfortunately, the code does not access "array.txt", so it never executes the code in the if condition.
Now I've heard that by using the fstream library it should work just fine, but the file is never found. I suspect that it doesn't work because it cannot find the path, but I have never opened files in C++. Also, I have not put my file anywhere in my project folder.
Some changes I've already tried:
file.open("array.txt");
omitting file.close();
include "C:\array.txt"; (with the # in front)
file.open("C:\array.txt")
And I'm using Windows 10, if this matters.
#include <iostream>;
#include <string>;
#include <vector>;
#include <fstream>;
//#include <"C:\Users\Samer El-Hage\Documents">;
using namespace std;
void main(){
vector<string> v (10);
ifstream file;
file.open("C:\array.txt", ios::in);
if (file.is_open())
{
for (int i = 0; i < 3; i++)
{
file >> v[i];
}
file.close();
}
else cout << "Could not access file.";
for (int i = 0; i < 3; i++)
{
cout << v[i] << " ";
}
}
This code prints "Could not access file."
The file cannot be opened because the file system can't find the file named "[Bell]rray.txt". the character sequence '\a' is the "Make my computer Beep" character.
Use either forward slashes: "C:/array.txt", an escaped backslash: "C:\\array.txt" or a raw string literal: R"(C:\array.txt)"
The file must also exist at the specified location. If you do not provide a drive and just say "array.txt" the location defaults to wherever the executable is (or in an IDE, the Working Directory).
Also, you have unnecessary semi-colons after your includes. (In fact, in a Treat Warnings as Errors setup, this won't compile!)
I got it! I had not put the .txt file in my folder with the source code, which, strangely enough, was not mentioned in my previous search results... I got to search better!
\a simply turns the computer beep on. Try writing "C:\\array.txt" instead in the open call.
Try not calling open explicitly:
ifstream file ("array.txt");
Look at the examples here:1
Ok, so its been a while since i messed with reading and writing file and i have just about forgot everything i learned. So, i am currently just trying to figure out how to read specific lines from a text file and output that said line into the command prompt. Here is my code that i am having issues with:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream input;
int lineN=0;
string line[lineN];
input.open("input.txt");
getline(input, line[lineN]);
cout << line[lineN];
}
As it currently is, it will read the first line of the text file no problem. However, if i change the variable lineN(which stands for line number) to 1 to read the second line, it crashes the prompt. I have no idea what it is i am doing wrong. I have tried researching this problem, but everyone's answer is too vague (That or i'm just too dumb). If you could help me out that would great.
The problem is that you define here an empty array of strings and arrays are not dynamic:
int lineN=0;
string line[lineN];
When you change lineN to 1, nothing changes in the array, and you'll get out of bound !
The bettter way would be to use vectors:
vector<string> line;
Read in a temporary string:
string current_line;
getline(input, current_line);
and add it to your vector:
line.push_back(current_line);
Putting all this in a nice loop would be more useful:
string current_line;
while (getline(input, current_line)) {
line.push_back(current_line);
}
You may access any line later, by using line[i] exactly with your array, as long as i< line.size(). Or you may iterate easily throug all its content:
for (string x : line) { // means for every x in line[]
cout<< x<<endl;
}
you allocate a array of size 0 ...
you will find answer of what will happen can be found here:
C++ new int[0] -- will it allocate memory?
Im a about to develop a program that will prompt user to input numbers and the program will make several calculation and will store the output datas into a csv file.
My question is :
How can I copy and store the data into a new .csv file instead of the existing one each and every time after:
1) I quit and restart the program
2) When the program suddenly terminates while running(example:My computer suddenly turns off while im running the program due to external errors)?
P.S:Please solve or explain to me with sample codes instead of just words cz im new to c++.
Thanks in advance :)
#include <iostream>
#include <fstream>
#include<string>
#include <iomanip>
using namespace std;
int main()
{
float a,b,c;
ifstream indata;
ofstream outdata;
outdata.open("Out.csv", ios::app);//opens the csv file
outdata << "Num1,Num2,Answer" << endl;//'prompt user for numbers
while (!(a ==-100))//calculate the product of the numbers
{
cout<<"Enter Num1:";
cin>>a;
if(a==-100)break;
cout<<"Enter Num2:";
cin>>b;
c=a*b;
outdata<<a<<","<<b<<","<<c<< endl;
}
indata.open("out.csv");//stores data into the csv file
system("pause");
return 0;
}
1) Assemble the filename from the current date and time.
2) If the hardware suddenly stops working or the process is forcibly terminated with kill -9 or Task manager, you get no notification and so you cannot reliably achieve what you seem to want.
'Please solve or explain to me with sample codes instead of just words cz im new to c++'. Get better by writing code and getting it to work:)
#include <time.h>
time_t now;
time(&now);
char outputFileName[128];
sprintf(outputFileName,"%ld_my_data.csv",now);
All I need to do is a simple read from a file in the same directory, but for some reason it refuses to work.
It works perfectly fine in this quick test one I made after I had problems, and outputs the number of entries in the text file.
#include <iostream>
using std::cout;
using std::cin;
#include <cstdio>
int main()
{
int a;
int b = 0;
freopen ("7.txt", "r", stdin);
while (cin >> a)
++b;
cin.clear();
fclose (stdin);
freopen ("7.txt", "r", stdin);
cout << b << '\n';
fclose (stdin);
}
EDIT: Wow I'm sorry to everyone who tried wrapping their heads around this. It was pretty late when I posted this, but I thought I finished. Apparently not. Now upon reopening my file to post the code in it, I realize that I moved everything into a folder before, but apparently when I tried to run the actual thing, it saved back outside of the folder, so it couldn't open "7.txt".
Problem solved I guess, sad waste of space seeing as how it wasn't even complete O_o, sorry.
EDIT2: Okay now I'm confused. I had a temp account on this computer, but when I logged into this account to ask a different question, this one as I meant to post it the other night showed up. I wasn't even on this computer while asking it. Not sure why it wasn't posted like that if it was all ready to be though.
My best "guess" is that you are trying to re-read the same file. If this is the case then you could try this :
std::ifstream file("7.txt");
std::string line = "";
while(std::getline(file, line))
{
//do something
}
//reset file pointer
file.clear();
file.seekg (0, std::ios::beg);
//re-read file
while(std::getline(file, line))
{
//do something else
}
Please try and formulate better questions in the future.
I have no idea what you're trying to do, but any interaction between freopen, fclose and cin is implementation defined at best (and most likely undefined behavior).
I solved the problem (Hoppity) as given on Facebook Puzzle Page. I solved it in c++ language (using g++ compiler) and mailed the .cpp file as an attachment to the mentioned e-mail address. I didn't zip the file. After few hours I received a mail regarding run/build error. Can anyone please help me with this. Where m I going wrong?
Here's the code I submitted:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
long long i,n,k;
ifstream fin("a.in");
ofstream fout("output.in");
fin>>n;
k=n/15;
for(i=0;i<k;i++)
{
fout<<"Hoppity"<<"\n";
fout<<"Hophop"<<"\n";
fout<<"Hoppity"<<"\n";
fout<<"Hoppity"<<"\n";
fout<<"Hophop"<<"\n";
fout<<"Hoppity"<<"\n";
fout<<"Hop"<<"\n";
}
for(i=k*15+1;i<=n;i++)
{
if(i%5==0) fout<<"Hophop"<<"\n";
else if(i%3==0) fout<<"Hoppity"<<"\n";
}
return 0;
}
What strikes me is that you do not take the name of your input file from the command line like the Hoppity puzzle demands. Instead, you read input from some "a.in" file.
Furthermore, you're expected to write the results to STDOUT, not some "output.in" file.