Hi; I want to add a string of numbers(e.g 9999) which are stored in a text file.

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

void main (void)
{
clrscr();
char ch;
int x=1;
long sum=0;
long integer;
FILE *fptr=fopen("thousand.txt","r");

while (ch!=EOF)
{
ch=fgetc(fptr);
ch= .....................I AM STUCK HERE..............

}
fclose(fptr);

getch();
}

The code looks more like C code than C++ code,

Are you sure that you are writing C++ code, Because there is a separate C forum,

You could ask for help there, I guess you should ask an admin to move this post in-to the C forum.

Nope it is Turbo C++!!!

What a pity, you are still struck at a 18 year old compiler.
Read this to migrate to a better compiler.
Sorry, But I am on a social movement about "anti old compilers" so I won't/can't help you.

commented: I hope somebody refuses to help you because you don't use their preferred compiler someday. Are you here to help, or are you here to bash people for your 'social movement'? -1
commented: Works for me!!!!! +19
Member Avatar for jencas

Nope it is Turbo C++!!!

No, it's pure plain C compiled with a C++ compiler.

I want to add a string of numbers(e.g 9999) which are stored in a text file.

Are you just adding the digits? If so then for each character that is a digit, you can subtract '0' from it to get the integer value:

'0' - '0' = 0
'1' - '0' = 1
'2' - '0' = 2
'3' - '0' = 3
'4' - '0' = 4
'5' - '0' = 5
'6' - '0' = 6
'7' - '0' = 7
'8' - '0' = 8
'9' - '0' = 9

But make sure that you only do this and add to the sum if the character is a digit. There might be whitespace in your file that can skew the sum. The best way to check for a digit is with the isdigit() function from <ctype.h> or <cctype> on newer C++ compilers.

One big problem with your code is the loop condition. First, ch is not initialized, and accessing an uninitialized variable on the first iteration invokes undefined behavior. Next, ch is a char type, and the char type is not guaranteed to be signed, but EOF is always a negative value. The loop might run forever. This is the idiomatic way to read characters in a loop from C:

int ch; // fgetc returns int

while ((ch = fgetc(fptr)) != EOF)
{
    /* ... */
}

And the obligatory portability comments. main returns int, but returning void won't work everywhere. clrscr() isn't a portable function and a lot of compilers won't support it, plus it's pointless to clear the screen at the beginning of the program. getch() isn't a portable function, and getchar() or cin.get() from C++ work just as well for stopping the program at the end.

So I think your confusion is in the fact that you're using a C++ compiler, but using C code in that compiler to open the file. While the method you're using is very common, there is a C++ method:

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
//those are the includes you should need..

std::string filename = "yourfile.txt";
std::ifstream infile;
infile.open(filename.c_str()); // open file 

char temp[1024];
while(!infile.eof()){
	infile.getline(temp, 1024); //This is if you only have one string per line
        infile >> temp; //this can be called for each string in the line (spaces separating)

        some_variable = atoi(temp);
        //you'll just need to figure out some way to handle the data
        //that you're getting from the file which is temporarily stored
}

infile.close();

Hope that helps!

If you want the C method

char iobuf[255];
long ints[255] = {0};
int i = 0;
// get each line of the file
while( fgets(iobuf, sizeof(iobuf), fptr) != NULL)
{
    ints[i] = atol(iobuf);    
}
while(!infile.eof()){

The problem with this condition is it is out of order. eof() does not return true until after an unsuccessful call has been made to read from the stream. You should do this test after the input call:

while (true)
{
    infile.getline(temp, 1024);

    if (infile.eof()) break;

    // ...
}

But the powers that be already figured out a better way. getline() returns a reference to the stream, and the stream has a conversion to a type that can be used as a loop condition:

while (infile.getline(temp, 1024))
{
    // ...
}
some_variable = atoi(temp);

Two things. First, to use atoi you should include <cstdlib> or <stdlib.h>. That's the only header it is required to be declared in. Second, atoi is a dangerous function because there's no way to check for failure before doing damage.

For C, strtol is a better choice, and for C++ string streams or Boost::lexical_cast are much safer and more flexible.

If you want the C method

char iobuf[255];
long ints[255] = {0};
int i = 0;
// get each line of the file
while( fgets(iobuf, sizeof(iobuf), fptr) != NULL)
{
    ints[i] = atol(iobuf);    
}

Hey AD, where are you increasing i ?
According to that code, all the elements after element 0 in the ints array will remain zero, only the first element (element 0) would be able to change.

So I would suggest to change this line: ints[i] = atol(iobuf); to: ints[i++] = atol(iobuf); :)

commented: Good catch :) +36

Hey AD, where are you increasing i ?

Oops! Nice catch :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.