Solved

help needed ( urgent )

Posted on 1997-05-01
1
190 Views
Last Modified: 2012-05-04
I have the following code that reads in a .txt file and stores it into an array.

A typical .txt file format that I would want to read in is :

6
22533
17414
40266
2048
1
0
5
33
45

etc...

Now the little program I have reads the file and stores each line as characters ( I THINK ! ). This is no good since I can not perform any mathematical operation on the contents of the array.

Does anyone know how to modify the program so that it reads in the file and stores the contens as numbers in the array.

The code is
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#define MAX_LINE 20

void main( void )
{
     FILE *stream;
     char line[MAX_LINE];
     char *array[1000];
      int nCount = 0;
      const int ArSize = 12 ;
      char file[ArSize] ;
      cout<< "Enter file name to load please : " ;
      cin >> file ;
                           

     if( (stream = fopen( file, "r" )) != NULL )
     {
         while ( fgets( line, MAX_LINE, stream ) != NULL)
         {
                               
            array[nCount] = new char[strlen(line)+1];
            strcpy(array[nCount], line);
            nCount++;
                     }
           fclose( stream );
           }

          printf("readed lines %d\n", nCount);

// Don't for get to delete all the Array elements before leaving!

      cout << array[8] << "\n"
}


Cheers...
0
Comment
Question by:9628657
1 Comment
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 20 total points
ID: 1163135
I changed your program so that it uses an Array of int instead of an array of char*. I used atoi to convert the string to a int.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>   // FOR atoi
#include <iostream.h>
#define MAX_LINE 20

void main( void )
{
 FILE *stream;
 char line[MAX_LINE];
 int array[1000];     // CHANGED ARRAY-TYPE TO INT
 int nCount = 0;
 const int ArSize = 12 ;
 char file[ArSize] ;
 cout<< "Enter file name to load please : " ;
 cin >> file ;
                     

 if( (stream = fopen( file, "r" )) != NULL )
 {
   while ( fgets( line, MAX_LINE, stream ) != NULL)
     // USED atoi to convert string to integer
     array[nCount++] = atoi(line);
   fclose( stream );
 }
}
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now