Solved

help needed ( urgent )

Posted on 1997-05-01
1
197 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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.
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.

919 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

21 Experts available now in Live!

Get 1:1 Help Now