Hi everyone!

I'm triyng to develop a program whith this functions:

1 - Read data from text file
2 - Put the data from file to a variable(ignoring comments in the file)
3 - Tokenize the data e transform the variable into another variable(like float)
4 - Make the calcs and put the data in another variable.
5 - Write the variable in another file

I'm look in various treads,site,books but I don't find especific what i'm look for.I make some progress:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<fstream.h>
#include<iostream>
using namespace std;

int main () {
  char c, str[3];
  ifstream is;

  cout << "Enter the name of an existing text file: ";
  cin.get (str,256);

  is.open (str);        // open file

  while (is.good())     // loop while extraction from file is possible
  {
    c = is.get();       // get character from file
    if (is.good())
       
      cout << c;
  }

  is.close();           // close file
  
  getch();
  return 0;
}

This code read the imput text file and write in the screen the values, but I wanna tokenize/split this values, and transform then into float(once they are char).

The file I read is this:

#S 1
1 50 30 25 68

I wanna compare the #S 1 to another value pre-determined, and tokenize the value in the down line...

Can you help me?

Thanks and Sorry my english =]

line 13: variable str was declared on line 3 to have only 3 characters, yet you expect someone to type up to 256???? Also, use cin.getline(), not cin.get() because getline() will allow you to enter spaces.


The syntax in the loop is incorrect. ifstrezam will make all conversions for you if you use >> operator.

float num;
std::string line;
getline(is,line); // get the first line, e.g. #S 1
while( is >> num )
{
   cout << num << '\n';
}

Edited 5 Years Ago by Ancient Dragon: n/a

hi ancient, thanks for the help... the string are declared wrong I corrected,but the second part i don't understand...

so, the program is runing, and reading the data, how i can tokenizer/split the data, anda the cin >> will convert then to float?

thanks again

The string line is correct -- its derived from std::string declared in <string> header file. You should compile and run the program yourself to see what >> does.

#include <iostream>
#include <fstream>
#include <string>

int main () {
  std::string line;
  std::ifstream is;
  float num;
  std::cout << "Enter the name of an existing text file: ";
  std::getline(std::cin, line);
  is.open (line.c_str());        // open file

  // get the first line
  std::getline(is,line);
  // how get all the rest of the numbers
  while( is >> num)
  {
     std::cout << num << '\n';
  }
  is.close();           // close file
  
  cin.get();
  return 0;
}

Edited 5 Years Ago by Ancient Dragon: n/a

ohh, ok ancient the >> make read just the numbers,and not the line who begins with #(char variable) correct?

see if i'm correct, now the program is opening the file and get the numbers of the lines whith no "#", but it is separete the numbers?

because what i triyng to do is read, get the numbers and separate they, to after make operations(+,-) with then...

sorry my stupid, but i learn just the basic of c in school,

thanks a lot

>>ohh, ok ancient the >> make read just the numbers,and not the line who begins with #(char variable) correct?

Yes, and that's why I added the getline() just before the while loop

What I posted will just read all the numbers, one at a time. You will have to add code that uses those numbers for something. I don't understand what you need to do with those numbers.

Edited 5 Years Ago by Ancient Dragon: n/a

hmm ok ok, mas in what variable is that numbers?

like "float num = a + 3"

you can understand me?

thanks

Did you compile and run that little program? If not, you should do it now so that you gain better understanding of what it does.

yes ancient I compile and run, and I observe: the numbers are read a packed togheter, so i make an ancount like this:

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
using namespace std;


int main () {
  std::string line;
  std::ifstream is;
  float num,num1;
  std::cout << "Enter the name of an existing text file: ";
  std::getline(std::cin, line);
  is.open (line.c_str());        // open file


  // get the first line
  std::getline(is,line);
  // how get all the rest of the numbers
  while( is >> num)
  {
     num1 = num + 50;    
     std::cout << num1 << '\n';
  }
  is.close();           // close file
  
  cin.get();
  return 0;
}

the return was the numbers increase by 50, this is what i'm loking for, but i wanna separate that numbers to make operations with them...

Hi ancient, i make some progress, my code is looking like this:

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <sstream>
using namespace std;


int main () {
  std::string line;
  std::ifstream is;
  float num,num1;
  std::cout << "Enter the name of an existing text file: ";
  std::getline(std::cin, line);
  is.open (line.c_str());        // open file


  while (is.good())     // loop while extraction from file is possible
  {
     string str;
     getline (is,str);
     // tokenizing
     stringstream s(str);               // make string stream
     int i, j;
     float data[20];
     // read up to 20 data elements from stringstream while stream is good (no errors) 
     for(i=0; i<20 && s.good();i++)    
     s >> data[i];
     cout << "i = " << i << endl;      // print number of elements read
     for(j=0; j<i;j++)
     cout << "data[" << j << "] = " << data[j] <<endl;
  }

  is.close();           // close file
  
  cin.get();
  return 0;
}

Now, I inform the file name, the program read, and split/tokenize the data, now I have another question, when split the data the program toke line-by-line and split then(what I wan't) but how I use this splits "data[1],data[2]" to do mathematics operations, because each line gerate datas[1], so how I know the data[1] is from the line 1, and the data[1] is from line 2, sucessivily?

Thank's

If you want to keep the data for each line separated then use a 2d array or 2d vector and declare it above that while loop that starts on line 20. float data[25][20]; is large enough to hold the data for 25 lines of code. Change the 25 to whatever you need for the data file the program is reading.

Hi again ancient, sorry for my no reply...

I understand the 2d array concept(or the vector), but i don't unferstand how I use.

I wanna to make this:

Catch the values yet separated line-by-line and create an matrix for each line. So all names of the element of the matrix will be the same, for example, the term A11 of matrixline1 i can multiply for the term A11 of the matrixline2.

You undestand? If possible try to put what you do in the code file, for a better look to me.

Thanks

All work: the final code is:

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <sstream>
using namespace std;


float data[100000][14]={{0.00}};

int main () {
  std::string line;
  std::ifstream is;
  float num,num1;
  std::cout << "Enter the name of an existing text file: ";
  std::getline(std::cin, line);
  is.open (line.c_str());        // open file
  if(!is.good())
    {
         cout << "openfail "; return 0;
    }

  int count=0;
  while (is.good())     // loop while extraction from file is possible
  {
     string str;
     getline (is,str);
     cout << str << endl;
     // tokenizing
     stringstream s(str);               // make string stream
     int i=0;
     // read up to 20 data elements from stringstream while stream is good (no errors)
     while(i<14 && s.good())
         {
         s >> data[count][i];
         if(!s.fail())i++ ;
         }
           // print number of elements read
     if(i != 0) count++;                // increment array index
  }

  is.close();           // close file
  // print the resultant array
  for(int i=0; i<count;i++)
    {
    for(int j=0; j<14;j++)
         cout <<  data[i][j] << " ";
    cout << endl;
    }

  cin.get();
  return 0;
}
This question has already been answered. Start a new discussion instead.