How can I read txt file

I need to read a txt file that contains the following data:

61 56 56 47 39 58 4 22

Basically it has 3 lines with different numbers, all separated by single space.

I have tried fgetc but it doesn't seem to work. I would prefer using basic C/C++ functions or commands.
trubiatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nagubalaCommented:

#include <stdio.h>

FILE *stream;

void main( void )
{
     int Number,i;

   stream = fopen( "C:\\input.txt", "r+" );
   if( stream == NULL )
      printf( "The file was not opened\n" );
   else
   {
        do
        {
             /* Read data from file: */
            i = fscanf( stream, "%d", &Number );

            /* Output data read: */
            printf( "%d\n", Number );
        } while (i == 1);

      fclose( stream );
   }
}


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mnashadkaCommented:
In C++, use the ifstream file stream class, and the stream extraction operator >>.
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  int numbers[9] = {0};
  ifstream input("c:\\input.txt");
  input >> numbers[0] >> numbers[1] >> numbers[2]
        >> numbers[3] >> numbers[4] >> numbers[5]
        >> numbers[6] >> numbers[7] >> numbers[8];
  return 0;
}

Good luck.
0
SalteCommented:
If you want to use basic C functions why don't you ask in the C group?

Anyway, fgetc() should work fine, when you get a digit, start saving them in a buffer, when you then get a non-digit you do atoi() on that buffer and save the value and then you scan past the non-digits until next digit etc. stop when EOF is read.

Alf
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Rapture273Commented:
If you want to read in, it will be easier if you read into an array (looking at mnashadka's example). To do this, you can simply use a loop.

for (int i = 0, i < num_elements - 1, i++)
{
    cin>>numbers[i];
}

where num_elements is the number of numbers you are trying to read in, and numbers is the array that you will store them to.
0
udilCommented:
This question has been abandoned. I will make a recommendation to the moderators on its resolution in a week or two. I appreciate any comments that would help me to make a recommendation.

In the absence of responses, I may recommend DELETE unless it is clear to me that it has value as a PAQ. Silence = you don't care.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Udil
EE Cleanup Volunteer
0
Kyle AbrahamsSenior .Net DeveloperCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept  nagubala answer.

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Ged
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.