How to store data of txt file into an array? [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 days ago.
Improve this question
int main()
{
string array[4000];
short loop = 0;
string line;
ifstream myfile("out0.txt");
if (myfile.is_open())
{
while (!myfile.eof())////
{
getline(myfile, line);
array[loop] = line;
cout << array[loop] << endl;
loop++;
}
myfile.close();
}
else cout << "can't open the file";
}
I have a txt file which contains data as:
12.4
45.2
65.2
44.3
...
...
...
I want to read this column in an array. Above is the code but its not giving the desired output kindly help

Related

How can i read all files in directory in c++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
I want to read all files in specific directory.
for example
In directory a there are file1.text file2.txt a.txt c.txt
I want to know how many words are included in each file.
I made a code for a file.
But I don't know how to automatically move on next file at same directory.
int EBook::get_total_words()
{
ifstream ifs("inputs//a.txt");
int words = 0;
string word;
if (!ifs)
{
std::cout << "Unable to open file " << '\n';
exit(1);
}
while (ifs >> word) {
++words;
}
return words;
}
With std::filesystem you can do this:
std::string path_to_dir = '/some/path/';
for( const auto & entry : std::filesystem::directory_iterator( path_to_dir ) ){
std::cout << entry.path( ) << std::endl;
}

Counting number of words in a file using 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 4 years ago.
Improve this question
I have been asked to develop program to count no. of lines and words in the file, this is my trial, my teacher said that I cant use >> operator for counting words and comparing but I could not handle it.
#include <iostream>
#include <fstream>
using namespace std;
int numberLines = 0;
int numberWords = 0;
void numberOfLines(){
cout<<"number of lines is " << numberLines << endl;
}
void numberWords(){
cout << "number of words is " << numberWords <<endl;
}
int main(){
string line;
char a = '';
ifstream myfile("files.txt");
if(myfile.is_open()){
while(!myfile.eof()){
getline(myfile,line);
cout<< line << endl;
numberLines++;
}
if ( a == ' '){
NumberWords++;
}
}
myfile.close();
}
numberOfLines();
numberOfWords ();
}
What you can do is add a 3rd argument to getline(). This lets it pull data from the stream until it hits a character. Doing getline(cin, line, ' ')takes all the data until the next and puts it into line. Your code might look like:
while(getline(inFile, line))
{
++numlines;
stringstream lineStream(line);
while(getline(lineStream, line, ' '))
{
++numWords;
}
}
The outer loop goes through the file and stores each line into line, then the inner goes through that line and counts each space. which correlates to a word.

perform a specific function for each line read in c++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm want to write a program that will read from a file line by line. For each line read, I will perform a specific function and move to the next line.
What will be a general syntax for this operation in c++
int main(){
string line;
string instruction;
string input;
ifstream file("file.txt");
if (file.is_open())
{
while (getline(file, line))
{
// perform a function with info from first line
//den move to second line
}
file.close();
}
else cout << "Unable to open file";
return 0;
}
You're quite there, what you are missing is just the function that would process the line.
void processLine(const string& line) {
cout << line << '\n';
}
Then just as you already do:
while (getline(file, line))
{
processLine(line);
}

Can't write in file using C++ by tutorial [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 8 years ago.
Improve this question
Using the tutorial here. I can't write text into file. I don't know why, please help me.
The following is the code:
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
Where does the following fail?
Does a new file named "example.txt" get created?
Try adding a couple of cout statements in between your code to see how far you get.
ofstream myfile;
cout << "here 1\n";
myfile.open ("example.txt");
cout << "here 2\n";
myfile << "Writing this to a file.\n";
cout << "here 3\n";
myfile.close();
cout << "here 4\n";
return 0;
Your code looks just like the code in the tutorial so it should work, although I haven't tested it myself.

How to append text after a certain line number? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Say I have a text file containing 10 lines. I want to move to line #5, clear everything below it, and append some new texts after that. What is the most compact way to achieve this using C++ of stream (just in case I missed some ofstream features)?
Read N lines while writing to a second file, then write all the new text to the new file after that.
Use IOstream to open the file and store the first five lines in an array and recreate the test file using the array and whatever other lines you want. Here is a code example:
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
const int linesToRead = 5; //How many lines to read before stopping
string lines [linesToRead];
int line = 0;
ifstream myinputfile ("example.txt");
if (myinputfile.is_open())
{
while ( myinputfile.good() && line<=linesToRead )
{
if(line<linesToRead)
{ //Stop reading at line 5
getline (myinputfile,lines[line]);
cout << lines[line];
}
line++;
}
myinputfile.close();
}
else cout << "Unable to open file";
//Begin creating new file
const int numberOfNewLines = 7;
string newlines[numberOfNewLines] = {"These", "are", "some", "of", "the", "new", "lines"}; //lines to be added after the previous 5
ofstream myoutputfile ("example.txt");
if (myoutputfile.is_open())
{
for(int i = 0; i<linesToRead; i++){
myoutputfile << lines[i] << "\n";
}
for(int i = 0; i<numberOfNewLines; i++){
myoutputfile << newlines[i] << "\n";
}
myoutputfile.close();
}
else cout << "Unable to open file";
return 0;
}