469,647 Members | 1,796 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

check the end of line as character and skip it

6
Hi,

I have a file in this format :

0 3 - - 10 10
1 0 0 0 11 11


I want to read this file and store in a matrix those values:

M[0][0] = 0 ;
M[0][1] = 3;
M[0][2] = -1;

The algorithm will replace character '-' with -1 when it loads into a matrix.

The program that I though is :

Expand|Select|Wrap|Line Numbers
  1. int PolicyGraph[][];
  2.  
  3. i=0;
  4. j=0;
  5. char car;
  6.  
  7. ifstream inFile;
  8.  
  9. inFile.open("stand-tiger.95-2678.alpha");
  10. if (!inFile) {
  11.         cout << "Unable to open file";
  12.         exit(1); // terminate with error
  13. }
  14.  
  15. infile >> car;
  16. while (!infile.eof()) 
  17. {
  18.  
  19.     while car not endofline   
  20.     {
  21.     if ( car != ' ')    
  22.     {
  23.         if (car != '-')
  24.         {
  25.         car >> PolicyGraph[i][j] ;
  26.         }
  27.         else 
  28.         {
  29.         -1 >> PolicyGraph[i][j]  ;
  30.         }    
  31.     }    
  32.     inFile >> car;;    
  33.     j++;    
  34.     }
  35. if (car = endofline)
  36. {
  37.    Jump_Next_line();
  38.    inFile >> car;
  39.  
  40. i++;    
  41.  
  42. }
  43.  
  44. ////////////////
I want to go reading char by char and load into matrix, skip whitespaces and end of lines, how to skip end of line ? std::getline gets all the line, and what I want is when program reads end of line then jump to the next line and continue reading the file.

Thanks

Borja
Jul 8 '08 #1
9 7694
arnaudk
424 256MB
Please use [i][code=cpp[/i]]...[/code] tags.

The easiest way to do what you say is to change the delimiter character for getline from '\n' to '', see the documentation for getline. Then it will keep reading until the end of the file. You'll have to ignore the newline characters when you parse the resultant string to put it in your matrix. But why can't you just loop over the file, line by line, adding each line to your matrix one at a time?
Jul 8 '08 #2
stomba
6
But the problem is that if you change the delimiter from '\n' to '' then how to control the end of line?

As I said before, I want to load the file into a matrix, so a line of the file is a row in my matrix and so on... how to loop over the file, line by line and add each element of the line to my matrix?
Jul 8 '08 #3
arnaudk
424 256MB
Parse the file line by line, something like this:
Expand|Select|Wrap|Line Numbers
  1. int i=0;
  2. string line;
  3. while ( getline(inFile, line) ) // this loop will exit when EOF reached
  4. {
  5.   for(int j = 0; j < line.size(); j++)
  6.   {
  7.     // parse line[j] and put it into PolicyGraph[i][j]
  8.   }
  9.   ++i; // increment row number
  10. }
  11.  
Unless you use dynamic arrays like std::vector or a matrix class, you'll need to know in advance the size of your array; it should be have at least line.size() columns and as many rows are there are lines in the file.
Jul 8 '08 #4
stomba
6
Yes! you are right , the best will be to use dynamic array bz it changes from one file to another ;)

what you get doing getline is the size of the string saved on line, but if all the integers are of one digit there is not a problem, you read as you said and then copy into a matrix. But as you can see in my example:

0 3 - - 10 10
1 0 0 0 11 11

The 5th element is an integer (10). If you analyze char by char then it will copy to the matrix as 1 and 0 not as 10...
Jul 8 '08 #5
stomba
6
I have the solution, if someone else have the same problem here it is:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <fstream>
  8. #include <sstream>
  9.  
  10. #include <vector>
  11.  
  12. using namespace std;
  13.  
  14. #define M 1000
  15. #define N 500
  16. typedef vector<int> row;
  17. typedef vector<row> matrix;
  18.  
  19. matrix readmatrix(string filename)
  20. {    
  21.     matrix mymatrix;
  22.  
  23.     ifstream myfile;
  24.     myfile.open(filename.c_str());
  25.  
  26.     while (!myfile.eof())
  27.     {
  28.         char rowstring[4096];
  29.            myfile.getline(rowstring,4096);
  30.  
  31.         istringstream iss;
  32.         iss.str (rowstring);
  33.  
  34.         row thisrow;
  35.         while (!iss.eof())
  36.         {
  37.  
  38.                    int val;
  39.            string token;
  40.            iss >> token;
  41.            if (token == "-")
  42.            {
  43.             val = -1; 
  44.            }
  45.            else
  46.            {
  47.               val = atoi(token.c_str());
  48.            }
  49.  
  50.            thisrow.push_back(val);
  51.         }
  52.         thisrow.pop_back();
  53.         mymatrix.push_back(thisrow);
  54.     }
  55.     mymatrix.pop_back();
  56.  
  57.  
  58.     myfile.close();
  59.  
  60.     return mymatrix;
  61. }
  62.  
  63.  
  64.  
  65. void displaymatrix(matrix m)
  66. {
  67.     for (int i=0;i<m.size();i++)
  68.     {
  69.         for (int j=0;j<m[i].size();j++)
  70.         {
  71.             cout << m[i][j]<<" ";
  72.         }        
  73.         cout<<"\r\n";
  74.     }
  75. }
  76.  
  77. main ()
  78. {
  79.     displaymatrix(readmatrix(filename));
  80.  
  81.     return 0;
  82. }
  83.  
  84.  
Jul 9 '08 #6
oler1s
671 Expert 512MB
<cstdlib> not <stdlib.h>
<cstdio>
You are missing <string>

Don't use defines for constants. Create actual global constants. Use the keyword const.

main:
main returns an int. It is int main. Always. How did your code compile?
displaymatrix(readmatrix(filename)); Show me where you define and initialize filename.

displaymatrix:
cout << "\r\n"; Only \n needed, which will be translated to the OS EOL sequence.

readmatrix:
myfile.eof() does not do what you think it does. Remember you have a stream of data. Not a fixed block. And computers are not psychic.
char rowstring[4096]; Not needed. Use a std::string. To use getline with strings, use the getline in <string>, not fstream::getline()

val = atoi(token.c_str()); Redundant. Use stringstream to push into an integer and check if the operation succeeds.
Jul 9 '08 #7
stomba
6
<cstdlib> not <stdlib.h> OK
<cstdio>
You are missing <string>

Does not matter...

Don't use defines for constants. Create actual global constants. Use the keyword const.

OK

main:
main returns an int. It is int main. Always. How did your code compile?
displaymatrix(readmatrix(filename)); Show me where you define and initialize filename.

It compiles perfectly

File is in the same folder as this code, so where says filename it has to be the name and extension of the file.


displaymatrix:
cout << "\r\n"; Only \n needed, which will be translated to the OS EOL sequence.

SURE

readmatrix:
myfile.eof() does not do what you think it does. Remember you have a stream of data. Not a fixed block. And computers are not psychic.
char rowstring[4096]; Not needed. Use a std::string. To use getline with strings, use the getline in <string>, not fstream::getline()

OK, I will change ;)

val = atoi(token.c_str()); Redundant. Use stringstream to push into an integer and check if the operation succeeds.

OK
Jul 9 '08 #8
oler1s
671 Expert 512MB
You are missing <string>
Does not matter...
Um...what? You use std::string, right? Then include <string>.

It compiles perfectly (missing int for main)
Get a better compiler? You can't omit the return type on a function, and main must return an int.

File is in the same folder as this code, so where says filename it has to be the name and extension of the file.
That's not what I asked. In your code you have the word filename. This is not a C++ keyword. So it must resolve to something else you made, like a class, a struct, a functionname, or a variable identifier. You must define said symbol, and then give a value to it. Show me where you do that.
Jul 9 '08 #9
stomba
6
Sorry I have adde string but I forgot to put into the code : those are the packages that I add, maybe some of them are not neccesary:


#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>



Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. main ()
  4. {
  5.     displaymatrix(readmatrix(filename));
  6.  
  7.     return 0;
  8. }
  9.  
  10.  
There I did the return, what I did not put is int main , but it works anyway... I do not know if I understand your sentence...

That's not what I asked. In your code you have the word filename. This is not a C++ keyword. So it must resolve to something else you made, like a class, a struct, a functionname, or a variable identifier. You must define said symbol, and then give a value to it. Show me where you do that.


I just give the name of the file: "array.txt" , where I said filename...

Borja
Jul 11 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by yawnmoth | last post: by
3 posts views Thread by Yazar Yolait | last post: by
4 posts views Thread by Surya Kiran | last post: by
8 posts views Thread by nick | last post: by
9 posts views Thread by Adi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.