-4

I am facing a problem with C++ and C, where my ifstream object, or file pointer is not reading a text file properly, and displays illegal characters when output. However, when I read a .dat file, it outputs the correct result.

This is the C code:

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main() {
    FILE *file;
    char ch;
    file = fopen("code.dat", "r");
    while((ch = getc(file)) != EOF)
        printf("%c", ch);
    getch();
    fclose(file);
}

This is the CPP code:

#include <fstream.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>

int main() {
    clrscr();
    fstream file;
    file.open("code.dat", ios::in);
    char ch, c;
    char token[6];
    int id = 0, op = 0, key = 0;
    while (!file.eof()) {
        file >> ch;
        if(ch == ' ') {
            if ((ch > 64 && ch < 91) || (ch > 96 && ch < 123))
                id += 1;
        }
    }

    cout << id;
    file.close();
    getch();
    return 0;
}
hnefatl
  • 5,860
  • 2
  • 27
  • 49
  • 5
    Turbo C++ is an ancient compiler, with discontinued support. It uses a dialect of C++ from before the standardization of the language. If you want to learn C++, the way it's used toady, upgrade to something modern. GCC and Clang are good options. – StoryTeller - Unslander Monica Jul 17 '17 at 14:12
  • 2
    "I am facing a problem with C++ and C, where my ifstream object" - You don't. C does not have an "ifstream object". It is a completely different language! – too honest for this site Jul 17 '17 at 14:12
  • Possible duplicate of [Why doesn't a simple "Hello World"-style program compile with Turbo C++?](https://stackoverflow.com/questions/44863062/why-doesnt-a-simple-hello-world-style-program-compile-with-turbo-c) – Richard Critten Jul 17 '17 at 14:27
  • @Olaf The file pointer for C, and ifstream object for C++.. I know that much bro.. – Akhilesh Iyer Jul 21 '17 at 18:09
  • @AkhileshIyer: Well, you at least seem not to knwo [ask]. (Oh, and: I'd appreciate if you refrain from using street-slang … "bro"). – too honest for this site Jul 21 '17 at 20:00

2 Answers2

0

A potential issue could be in the code:

if(ch == ' ')
{
    if((ch > 64 && ch < 91) || (ch > 96 && ch < 123))
        id += 1;
}

The outer if statement excludes the possibility of the inner if statement executing (ASCII code for a space is 32, so ch can't be both 32 and satisfy one of the two conditions, so id is never incremented).

This doesn't seem like it would produce the behaviour you're describing though, it should just result in printing 0 to stdout.

It's hard to know what's going wrong without an example of output - we need an MCVe to provide good advice. Here, your C code just prints the contents of the file, whereas the C++ code counts the number of alphabet characters (maybe, I just skimmed over it). So which one fails? And how? Give us an example of the output, and clarify what you expected each one to do.

As others have mentioned, Turbo C++ is outdated - you should start using g++ or clang instead.

hnefatl
  • 5,860
  • 2
  • 27
  • 49
0

Since you are having problems with text file, try opening it in binary mode, ie:adding ios::binary. So the code becomes:

file.open("code.txt",ios::in|ios::binary);

Also file>>ch is used when you want to read an entire word. Since you want to read character by character try

file.get(ch);