Solved

help needed ( urgent )

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
White board coding practice 3 91
Error creating a new C++ project in ,net 20 42
Why  my code (program) build with old compiler? 11 77
Adding items to a C# list incrementally 5 33
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.

828 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