How to handle data or char -1 when reading from file, since EOF is also -1

How to handle data or char -1 when reading from file, since EOF is also -1

By : Sarbojit Das
Date : October 25 2020, 07:10 PM
seems to work fine The standard C character input functions return a value that is either an unsigned char or EOF. Thus, to use the return value from a function like fgetc, store it in an int, not char:
code :
int x = fgetc(stdin);
if (x == EOF)
    // Code for handling error or end of file.
    // Code for handling a character.

Share : facebook icon twitter icon
Reading from File Containing Int and Char data types

Reading from File Containing Int and Char data types

By : nvrgivup
Date : March 29 2020, 07:55 AM
will help you If you're just getting started with I/O in C++, you might bookmark a reference site, because the library provides a lot of options and features.
cppreference.com is a non-authoritative but handy source, wiki-style. Cay Horstmann wrote an article which covers more ground, and you can search for key words. cplusplus.com has fairly good coverage of older language features, but has a few errors and isn't well updated.
C++ reading data from a file into a char*

C++ reading data from a file into a char*

By : David Hemmingsson
Date : March 29 2020, 07:55 AM
this one helps. Since you use the standard library (cout) why not use string instead of char* ?
code :
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

fstream level;
string row;
int i = 0;
string data[3];

while ((i<3) && (level >> row)) {
    data[i] = row;
    cout << i << " " << data[i] << "\n";

for (i = 0; i < 3; i++) {
    cout << "\n " << i << " = " << data[i];

Reading from DATA file handle

Reading from DATA file handle

By : user3814520
Date : March 29 2020, 07:55 AM
like below fixes the issue I did some benchmarking against three methods. I used an external file for reading (instead of __DATA__). The file consisted of 3 million lines of the exact data you were using.
The methods are slurping the file, reading the file line-by line, and using Storable as Sobrique mentioned above. Each task is run 100 times. Here are the results, which show that once stored using Storable, it is by far much faster than the other two (118% faster than by line, and 45% faster than by slurping):
code :
           Rate  by_line by_slurp by_store
by_line  1.08/s       --     -33%     -54%
by_slurp 1.62/s      50%       --     -31%
by_store 2.36/s     118%      45%       --

use warnings;
use strict;

use Benchmark qw(cmpthese timethese);
use Storable;

my $file = 'in.txt';


cmpthese(100, {
    'by_line' => \&by_line,
    'by_slurp' => \&by_slurp,
    'by_store' => \&by_store,

sub by_line {

    open my $fh, '<', $file
      or die "Can't open $file: $!";

    my @ref_data;

    for my $line (<$fh>){
        push @ref_data, $line;

sub by_slurp {

    open my $fh, '<', $file
      or die "Can't open $file: $!";

    my @ref_data = <$fh>;

sub storeit {
    open my $fh, '<', $file
      or die "Can't open $file: $!";

    my @ref_data = <$fh>;
    close $fh;

    store \@ref_data, 'store.dat';

sub by_store{

    my @ref_data = retrieve('store.dat');
reading char data from binary file with numpy

reading char data from binary file with numpy

By : Abhishek Apte
Date : March 29 2020, 07:55 AM
To fix the issue you can do The way you're doing it is fine. Here's how you can convert it to a string.
code :
strbits = np.fromfile(infile, dtype=np.int8, count=73)
a_string = ''.join([chr(item) for item in strbits])
Reading a text file with char data

Reading a text file with char data

By : zen32e
Date : March 29 2020, 07:55 AM
wish of those help I have a text file with data like: , If you wanna store it to a list:
Related Posts Related Posts :
  • dynamic memory for 2D char array
  • C compiler producing lightweight executeables
  • Cannot Open Shared Object cygmpfr-1.dll
  • How to access a structure member in a function that get it as void* type?
  • Replace for loop with formula
  • how to find if stack increases upwards or downwards?
  • Why does my homespun sizeof operator need a char* cast?
  • Setting exit status when creating core dump
  • Signal Handling in C
  • How to implement B+ Tree for file systems?
  • Stack storage at function call
  • Manipulate the Clipboard in C on Mac OS X
  • Problem with writing a hexadecimal string
  • Why does this code read all ' ' for the anything after the 4th character?
  • GUI for linux c
  • Where do I put all these function-like #defines, in C?
  • Padding in 24-bits rgb bitmap
  • how to acess and change variable of kernel space from user space
  • Can a conforming C implementation #define NULL to be something wacky
  • send() always interrupted by EPIPE
  • Socket Select with empty fd set
  • Coding self similar fractals in computer graphics using C
  • Forking with Pipes
  • sscanf + c99 not working on some platforms?
  • Problem with C function of type char pointer, can someone explain?
  • Macros in C.... please give the solution
  • Unix Piping using Fork and Dup
  • Any difference between these two while loops?
  • Hashtable implementation for C
  • Problems with Linked List in C
  • Stack Allocation in C
  • Random function stuck on one number
  • printf following zeros to a certain degree
  • Measuring CPU clocks consumed by a process
  • How is a CRC32 checksum calculated?
  • Tips on redefining a register bitfield in C
  • Converting a program from c to bash script
  • Static library not included in resulting LLVM executable
  • What is the meaning of "wild pointer" in C?
  • Reading DWORD from binary file
  • Efficient way to find divisibility
  • Whats the fastest, portable way to hash pointers we know are pointer aligned to a fixed size int?
  • Why does my summation program behaves weird?
  • How to determine values saved on the stack?
  • Thread scheduling C
  • C - converting to 2s complement
  • wireless networks c program
  • From where does the uboot get the information about flash, RTC, RAM in uboot/kernel development
  • printf behaviour in C
  • Measure size and way-order of L1 and L2 caches
  • Efficient copy of entire directory
  • Efficient way to create/unpack large bitfields in C?
  • getnameinfo prototype asks for sockaddr not sockaddr_in?
  • Linked List Sorting with Strings In C
  • Typecasting a floating value or using the math.h floor* functions?
  • Problem implementing sorting algorithm in C with an array of structs
  • How to copy a ram_base file to disk efficiently
  • How to fix casting a pointer to an integer?
  • How to round floating point numbers to the nearest integer in C?
  • PIC C - Sending 200 values over USB, but it only sends 25 or so of them
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk