?
Solved

help needed ( urgent )

Posted on 1997-05-01
1
Medium Priority
?
214 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
[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
1 Comment
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 40 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  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 …
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 clear a vector as well as how to detect empty vectors in C++.

765 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