Help reading in file

Matrix Array File

Page 1 of 1

12 Replies - 6983 Views - Last Post: 25 September 2010 - 04:10 PM Rate Topic: -----

#1 YES i failed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-April 10

Help reading in file

Posted 22 September 2010 - 01:12 PM

ok so i've never used c++ before, mostly java but the class im taking is teaching me basic principles that are found across all languages. so basically i need to write a program in c++, which shouldnt be hard since i know java concepts pretty well. Im having trouble finding the correct syntax on how i would be able to read a file in number by number. all the numbers in the file are double digits and each one has 1 space in between.

    #include <iostream>
    #include <fstream>
    using std::ifstream;
    #include <cstdlib> // for exit function
    
    //this program will read in 2 matrices and store them in two 2d arrays
    //the text file includes text other then the matrices

    int main()
    {
       ifstream indata; 
       int num; 

      indata.open("matrixFile.dat"); // opens the file
       if(!indata) { // file couldn't be opened
          cerr << "Error: file could not be opened" << endl;
          exit(1);
       }

      indata >> num;
       while ( !indata.eof() ) { // keep reading until end-of-file
          
       //this is where each number will get stored into the array

          indata >> num; // sets EOF flag if no value found
       }
       indata.close();
       cout << "End-of-file reached.." << endl;
       return 0;
    }




here is what a sample .dat file will look like

first matrix:

34 45 23
85 93 72
85 37 26

second matrix:

34 66 43
33 11 33
99 44 56



overall, what im looking for at this point is some manipulation code for reading in files that will ignore whitespace and characters that aren't digits, and if theres a way to read in digits as double digits.

thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Help reading in file

#2 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Help reading in file

Posted 22 September 2010 - 01:28 PM

The operator '>>' does just that. err, except ignoring characters that aren't digits.

But I will tell you reading until .eof() returns true often-times causes a bug at the end of the file.

std::ifstream inFile("somedata.txt");
std::string input;

while(inFile >> input)
{
  std::cout << input << ' ';
}
inFile.close();



you can use ifstream.ignore() to ignore until a character has been reached.

But it's kind of tricky to use.

This post has been edited by taylorc8: 22 September 2010 - 02:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 Seta00  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

Re: Help reading in file

Posted 22 September 2010 - 02:27 PM

I often go for the read-the-whole-line-and-then-parse-it approach, so IO details can be ignored.

This post has been edited by Seta00: 22 September 2010 - 02:42 PM

Was This Post Helpful? 2
  • +
  • -

#4 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Help reading in file

Posted 22 September 2010 - 02:36 PM

I somewhat agree with Seta00 that reading the whole line is a better approach.

Also parsing a string to an integer or other type is dead-simple in C++, use a "stringstream".

This post has been edited by taylorc8: 22 September 2010 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

#5 YES i failed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-April 10

Re: Help reading in file

Posted 22 September 2010 - 03:42 PM

thanks i will work on trying your guys' suggestions tonight
Was This Post Helpful? 0
  • +
  • -

#6 YES i failed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-April 10

Re: Help reading in file

Posted 24 September 2010 - 01:54 PM

could someone explain to me what these mean/do?


#include <fstream> //so u can open/close files?
#include <cstdlib> //so u can use exit()?
#include <iostream> //so u can use I/O features?

using std::ifstream; //? what dos ifstream do
using namespace std; //so u dont have to use std::cout or whatever it is? can some1 explain that too?

thanks!
Was This Post Helpful? 0
  • +
  • -

#7 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Help reading in file

Posted 24 September 2010 - 02:02 PM

Those are "include files".

You include a file that has functions and classes defined in it, like the "ifstream" class, for reading from a file.
Was This Post Helpful? 0
  • +
  • -

#8 YES i failed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-April 10

Re: Help reading in file

Posted 24 September 2010 - 02:05 PM

ok so they are like import in java

what about the using namespace std; ?

edit*
not sure how it works but i found out its so you dont have to keep typing std:: in front of things

This post has been edited by YES i failed: 24 September 2010 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Help reading in file

Posted 24 September 2010 - 02:06 PM

https://encrypted.go...earch?q=fstream
https://encrypted.go...earch?q=cstdlib
https://encrypted.go...arch?q=iostream
https://encrypted.go...arch?q=ifstream
https://encrypted.go...g+namespace+std
Was This Post Helpful? 0
  • +
  • -

#10 Seta00  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

Re: Help reading in file

Posted 24 September 2010 - 02:17 PM

View PostYES i failed, on 24 September 2010 - 05:05 PM, said:

ok so they are like import in java

what about the using namespace std; ?


You just need to know that, unlike Java, #include means literally copypaste the whole file specified in <> or "".
using namespace std means exactly what it says, you're using the namespace called std.
Namespaces are a feature of C++ that allow you to separate your code in logical blocks and name them.
Was This Post Helpful? 0
  • +
  • -

#11 YES i failed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-April 10

Re: Help reading in file

Posted 24 September 2010 - 04:53 PM

thanks for everyone's help so far, the program is coming along slowly.

does anyone know how to figure out how many strings are on each line of a data file?
Was This Post Helpful? 0
  • +
  • -

#12 Seta00  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

Re: Help reading in file

Posted 24 September 2010 - 05:29 PM

View PostYES i failed, on 24 September 2010 - 07:53 PM, said:

thanks for everyone's help so far, the program is coming along slowly.

does anyone know how to figure out how many strings are on each line of a data file?


Long story short, the whole file is one single string.
You decide in how many strings you want to divide it.
Was This Post Helpful? 0
  • +
  • -

#13 YES i failed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-April 10

Re: Help reading in file

Posted 25 September 2010 - 04:10 PM

my overall program was supposed to read in 2 matrices, multiply them, and then append the result matrix to the end of the file. i have completed the program now so this thread can be closed :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1