?
Solved

How can I read txt file

Posted on 2003-02-20
6
Medium Priority
?
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 40

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

777 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