Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How can I read txt file

Posted on 2003-02-20
6
Medium Priority
?
230 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:trubiat
6 Comments
 

Accepted Solution

by:
nagubala earned 200 total points
ID: 7991504

#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
 
LVL 8

Expert Comment

by:mnashadka
ID: 7991523
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
 
LVL 12

Expert Comment

by:Salte
ID: 7992815
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Expert Comment

by:Rapture273
ID: 8010054
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
 
LVL 2

Expert Comment

by:udil
ID: 8868893
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
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 9443195
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question