Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey! I am trying to get my file to be read and printed on the screen, i am stuck at it

What I have tried:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <iostream>     // std::cout
#include <functional>   // std::minus
#include <numeric>

int main() {
    std::cout << "Please enter input file name: ";

    std::string iname;

    std::cin >> iname;

    std::ifstream ist {iname};          // ist is an input stream for the file named name
    std::ifstream f("");
    {
        if (f.is_open())
            std::cout << f.rdbuf();
        
    }
    std::cin.get();

}
Posted
Updated 17-Oct-19 3:04am
Comments
Member 14621201 16-Oct-19 16:18pm    
the columns are like this:
A Z TKE Y Yerror
118 46 140.0 0.3326E-03 0.6799E-04
118 45 140.0 0.9057E-04 0.1851E-04
118 47 140.0 0.9122E-04 0.1864E-04
119 46 140.0 0.7077E-04 0.2217E-04
119 45 140.0 0.5748E-05 0.1800E-05
119 47 140.0 0.6486E-04 0.2032E-04
120 47 140.0 0.3259E-03 0.7113E-04
120 46 140.0 0.1016E-03 0.2217E-04
120 48 140.0 0.7759E-04 0.1694E-04

and i need to add 3rd column the Y for each A (A runs from 118 to 165), for all 118 in the lines to keep adding the Y
Patrice T 16-Oct-19 16:35pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Member 14621201 16-Oct-19 17:48pm    
ok i will do it asap

I wrote a small helper function which will help, have added comments to help you understand.

C++
// SWAMI KARUPPASWAMI THUNNAI

#include <iostream>
// As content of the file is in strings.
#include <string>
// For file IO
#include <fstream>
// For Matix storing
#include <vector>


std::vector<std::vector<double>> get_matrix(std::string file_path, char sep)
{
	std::vector<std::vector<double>> mat;
	std::ifstream file;
	file.open(file_path);
	// Throw exception if you want to
	if(!file.is_open())
	{
		return mat;
	}
	while(!file.eof())
	{
		std::string row;
		std::getline(file, row);
		std::vector<double> row_vec;
		std::string row_value = "";
		for(char c : row)
		{
			// If the character is seperator
			if(c == sep)
			{
				// Convert the string to double and append it and reset the string
				row_vec.push_back(std::stod(row_value));
				row_value = "";
			}
			else
			{
				row_value += c;
			}
		}
		// If the string is not empty append the last value [Last value generally gets ignored as there is no seperator afterwards]
		if(row_value.size() != 0)
		{
			row_vec.push_back(std::stod(row_value));
			row_value = "";
		}
		// Append the row now
		mat.push_back(row_vec);
	}
	file.close(); // Always close the opened file
	return mat;
}


int main()
{
	std::vector<std::vector<double>> mat = get_matrix("mat.txt", ' ');
	// Let's display our matrix
	for(auto i: mat)
	{
		for(auto j: i) std::cout << j << " ";
		std::cout << "\n";
	}
}


My file contents:

118 46 140.0
118 45 140.0
118 47 140.0


Output:

g++ -std=c++11 main.cpp -o test.exe


118 46 140
118 45 140
118 47 140



Note: Robustness is inversely proportional to accuracy [Code Complete 2]. This code might be accurate but it is not robust. Even a small white space might break this code.

Trimming the strings, and dealing with other things like shape is your own quest.
 
Share this answer
 
Comments
CPallini 17-Oct-19 3:34am    
5.
Visweswaran N 17-Oct-19 8:48am    
Thank you sir for your kind help!
Try also
C++
#include <iostream>
#include <vector>
#include <utility>
#include <map>
#include <numeric>

using namespace std;
int main()
{ 
  vector < pair < int, double> > vp;
  multimap<int, double> mm;
  string line; 
  getline(cin, line); // skip header line
  
  for (;;)
  { 
    int id,z;
    double tke, y, yerror;
    cin >> id >> z >> tke >> y >> yerror;
    if ( ! cin ) break;
    mm.emplace( id, y);
  }
  auto it_begin = mm.begin(); 
  while (it_begin != mm.end() )
  { 
    auto it_end = mm.upper_bound(it_begin->first);
    auto sumfun = [] (double a, pair < int, double> p) {return a + p.second;};

    double sum = accumulate(it_begin, it_end, 0.0, sumfun);
    vp.push_back( make_pair(it_begin->first, sum));
    it_begin = it_end;
  }
  
  cout << "id ysum" << endl;
  for (auto p : vp) 
    cout << p.first << " " << p.second << endl;
}
Run it redirecting your data file as standard input.

Used with the following input data
A Z TKE Y Yerror
118 46 140.0 0.3326E-03 0.6799E-04
118 45 140.0 0.9057E-04 0.1851E-04
118 47 140.0 0.9122E-04 0.1864E-04
119 46 140.0 0.7077E-04 0.2217E-04
119 45 140.0 0.5748E-05 0.1800E-05
119 47 140.0 0.6486E-04 0.2032E-04
120 47 140.0 0.3259E-03 0.7113E-04
120 46 140.0 0.1016E-03 0.2217E-04
120 48 140.0 0.7759E-04 0.1694E-04

It produces the output
id ysum
118 0.00051439
119 0.000141378
120 0.00050509
 
Share this answer
 
Comments
Member 14621201 17-Oct-19 15:41pm    
It looks perfect i will try to link with a file open so will ask which file to open will come back to you if i run in trouble...
Member 14621201 17-Oct-19 17:05pm    
ok so i have renamed my file instead cf.h still get some errors:

error C2059: syntax error: 'constant'
error C2143: syntax error: missing ';' before '{'
error C2065: 'mm': undeclared identifier
error C3536: 'it_end': cannot be used before it is initialized
error C2065: 'vp': undeclared identifier

THE CODE USED:
"#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include "conio.h"
#include "ctype.h"
#include "string.h"
#include <vector>
#include <utility>
#include
#include <numeric>
#include "cf.h"
#include <iomanip>
#include <fstream>

using namespace std;
int main()
{

//getline(cin, line); // skip header line

for (;;)
{
int id,z;
double tke, y, yerror;
cin >> id >> z >> tke >> y >> yerror;
if ( ! cin ) break;
mm.emplace( id, y);
}
auto it_begin = mm.begin();
while (it_begin != mm.end() )
{
auto it_end = mm.upper_bound(it_begin->first);
auto sumfun = [] (double a, pair < int, double> p) {return a + p.second;};

double sum = accumulate(it_begin, it_end, 0.0, sumfun);
vp.push_back( make_pair(it_begin->first, sum));
it_begin = it_end;
}

cout << "id ysum" << endl;
for (auto p : vp)
cout << p.first << " " << p.second << endl;
}"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900