Solved

help needed ( urgent )

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

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

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 clear a vector as well as how to detect empty vectors in C++.

691 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