reading data from a .txt file as input

reading data from a .txt file as input

Post by Brad Fonsec » Sat, 03 Nov 2001 09:58:07



Hello!

I would like to read in a list of data into my program instead of keyboard
input.  How does one do this?

Regards,

Brad Fonseca

 
 
 

reading data from a .txt file as input

Post by MWRo » Sun, 04 Nov 2001 09:13:48




Quote:>Hello!

>I would like to read in a list of data into my program instead of keyboard
>input.  How does one do this?

Open a file and read it.

If you look in the MSL C or the MSL C++ references you will see how to
do this.   If I knew if you wanted C or C++ I could give you more info.

Ron

--
 CodeWarrior for Macintosh, v7 and CodeWarrior for Windows, v7
                 are shipping now !!  
      Call  1-800-377-5416 US and Canada Only



 
 
 

reading data from a .txt file as input

Post by Brad Fonsec » Sun, 04 Nov 2001 17:16:20


Hello again!

Sorry, I'm working on a C project.  Where are the MCL C and MCL C++
references?

Thanks,

Brad Fonseca




> >Hello!

> >I would like to read in a list of data into my program instead of
keyboard
> >input.  How does one do this?

> Open a file and read it.

> If you look in the MSL C or the MSL C++ references you will see how to
> do this.   If I knew if you wanted C or C++ I could give you more info.

> Ron

> --
>  CodeWarrior for Macintosh, v7 and CodeWarrior for Windows, v7
>                  are shipping now !!
>       Call  1-800-377-5416 US and Canada Only



 
 
 

reading data from a .txt file as input

Post by <beif.. » Mon, 12 Nov 2001 07:01:32


Brad, Try this code:

#include <fstream.h>

int a;

fstream data;
data.open("filename.txt",ios::in);

data >> a;

data.close();

I've taught C++ using Codewarrior to high school students for 6 years
now and have figured out a lot of these mysteries.  Let me know if I
can help you in any other way.

Bill



> Hello!

> I would like to read in a list of data into my program instead of keyboard
> input.  How does one do this?

> Regards,

> Brad Fonseca

--
Bill Eifert, Computer Science Dept, Princeton High School
11080 Chester Road
Cincinnati, OH  45040

 
 
 

reading data from a .txt file as input

Post by Brad Fonsec » Tue, 13 Nov 2001 02:40:43


Is this C code or C++? Where should it go in the code? Within the main
function (except #include <fstream.h>)? Do you have a simple example I could
try?

Many Thanks,

Brad


> Brad, Try this code:

> #include <fstream.h>

> int a;

> fstream data;
> data.open("filename.txt",ios::in);

> data >> a;

> data.close();

> I've taught C++ using Codewarrior to high school students for 6 years
> now and have figured out a lot of these mysteries.  Let me know if I
> can help you in any other way.

> Bill



> > Hello!

> > I would like to read in a list of data into my program instead of
keyboard
> > input.  How does one do this?

> > Regards,

> > Brad Fonseca

> --
> Bill Eifert, Computer Science Dept, Princeton High School
> 11080 Chester Road
> Cincinnati, OH  45040


 
 
 

reading data from a .txt file as input

Post by Eifert, Bil » Tue, 13 Nov 2001 03:07:49


[[ This message was both posted and mailed: see
   the "To," "Cc," and "Newsgroups" headers for details. ]]

It's C++, not C.  The code for an entire short program is:

#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>

void main(void)
{ // begin main

// declarations
double Fahrenheit, Celsius;

// open the data file
fstream data;
data.open("Temperatures.txt",ios::in);
if (!data)
  { // begin error message
  cout << "The file could not be opened." << endl;
  cout << "No data will be processed." << endl;
  exit(1);
  } // end error message

// process all values in the data file
while (data >> Fahrenheit)
  { // begin processing all values

  // calculate the Celsius temperature
  Celsius = 5./9. * (Fahrenheit - 32);

  // display answers
  cout << Fahrenheit << "? F = " << Celsius << "? C" << endl;

  } // end processing all values

// close the data file
data.close();

Quote:} // end main

The data file Temperatures.txt should contain one double value per line.

Hope this helps.

Bill Eifert



> Brad, Try this code:

> #include <fstream.h>

> int a;

> fstream data;
> data.open("filename.txt",ios::in);

> data >> a;

> data.close();

> I've taught C++ using Codewarrior to high school students for 6 years
> now and have figured out a lot of these mysteries.  Let me know if I
> can help you in any other way.

> Bill



> > Hello!

> > I would like to read in a list of data into my program instead of keyboard
> > input.  How does one do this?

> > Regards,

> > Brad Fonseca

 
 
 

reading data from a .txt file as input

Post by MWRo » Wed, 14 Nov 2001 01:11:20




Quote:>Is this C code or C++? Where should it go in the code? Within the main
>function (except #include <fstream.h>)? Do you have a simple example I could
>try?

You might want to check the chapters in MSL C++ Reference  the items in
input and output are fully documented with example usage.  

Ron

--
            GeekWare !!!
http://www.metrowerksstore.com/metrowerks/geekware.html


 
 
 

reading data from a .txt file as input

Post by Wood » Fri, 16 Nov 2001 07:15:49



Quote:> [[ This message was both posted and mailed: see
>    the "To," "Cc," and "Newsgroups" headers for details. ]]

> It's C++, not C.  The code for an entire short program is:

> #include <iostream.h>
> #include <iomanip.h>
> #include <fstream.h>

> void main(void)
> { // begin main

<snip code>

I'm no expert, but I thought main() always has a return type of int
(at least in C++)?
Woody

 
 
 

reading data from a .txt file as input

Post by Bill Eifer » Fri, 16 Nov 2001 10:59:28


It doesn't have to.  Returning void is less confusing for beginning
programming students --- no need for a return at the end of main.
However, I think that the Mac version of Codewarrior does return an
integer return type for main -- but the windows version doesn't.



> > [[ This message was both posted and mailed: see
> >    the "To," "Cc," and "Newsgroups" headers for details. ]]

> > It's C++, not C.  The code for an entire short program is:

> > #include <iostream.h>
> > #include <iomanip.h>
> > #include <fstream.h>

> > void main(void)
> > { // begin main

> <snip code>

> I'm no expert, but I thought main() always has a return type of int
> (at least in C++)?
> Woody

 
 
 

reading data from a .txt file as input

Post by meero » Fri, 16 Nov 2001 10:58:32




Quote:>It doesn't have to.  Returning void is less confusing for beginning
>programming students --- no need for a return at the end of main.
>However, I think that the Mac version of Codewarrior does return an
>integer return type for main -- but the windows version doesn't.

int return of main is required by the C/C++ standards. Any code which
does not return int from main is not compliant with the C/C++ standards.

hth

meeroh
--
Hire me: <http://meeroh.org/hire.html>

 
 
 

reading data from a .txt file as input

Post by MWRo » Fri, 16 Nov 2001 13:21:10




Quote:>It doesn't have to.  Returning void is less confusing for beginning
>programming students --- no need for a return at the end of main.

Actually it is more confusing as it is not legal  The standards both C
and C++ now require  int main() or int main(int argc,  char *argv[]) or
a form of them.

Quote:>However, I think that the Mac version of Codewarrior does return an
>integer return type for main -- but the windows version doesn't.

The standards also say if there isn't an explicit return for main it
will automatically return zero.

Ron

--
            GeekWare !!!
http://www.metrowerksstore.com/metrowerks/geekware.html