Questions tagged [getc]

Anything related to C or C++ standard library functions `getc` (C) or `std::getc` (C++). These functions are used to read a single character from a stream.

Anything related to C or C++ standard library functions getc (defined in <stdio.h> C standard header) or std::getc (defined in <cstdio> C++ standard header). These functions are used to read a single character from a stream and they can also be implemented as C-preprocessor macros.

See CPPreference.com:

91 questions
0
votes
1 answer

Why is my loop creating extra linked list nodes?

I'm working with linkedlist. I want to create a loop that allows the user to add nodes to the list. My output always has two extra blank nodes. I believe it has something to do with the way I am using the input functions to both take input and…
0
votes
2 answers

How to read values from a CSV file with fgets() without skipping data?

I'm trying to read a CSV file where I have just double values separated with a comma. I'm using the char *fgets(char *str, int n, FILE *stream) function to read the rows. In the code, to finish the do-while loop, I'm using the getc() method to read…
Oguz Canbek
  • 61
  • 1
  • 7
0
votes
2 answers

Searching for a keyword in a txt file and logging it with C

I'm attempting to use C to search a file that contains C code. It is meant to search through the entire file, find certain keywords or characters (Such as looking for Ints, Longs, For loops, etc.) and logs them by incrementing a counter, as well as…
0
votes
2 answers

Getc over-read a \n character

I'm having some problems with this little function that can read a file: void ReadFile(char *name) { FILE *fr; int lenght, i; fr = fopen(name, "r"); //Open the file reader fseek(fr, 0, 2); //Set the pointer at the EOF lenght = ftell(fr); …
LeonardoA
  • 3
  • 1
  • 2
0
votes
1 answer

Copying file text in C using getc() and putc() - binary code in the output file

I created a file called "text.txt" with a string inside and I want to copy that string in another file called "copiaqui.txt". But there's a problem. In the output file, I found this : Why the program doesn't copy the string correctly? Code…
Stefano
  • 37
  • 3
0
votes
0 answers

Read all data of a file

In C++ I'm trying to completely read files, char by char, using stdio.h (fopen, getc, etc). I have done a program that creates an explorer window and count the letters (and the number of Byte) inside a selected file. It seems to work properly for…
Luckk93
  • 3
  • 3
0
votes
1 answer

How to display characters read from a file using getc()

When I try to read input from a file named "file1", my program correctly displays the number of characters in the file, but in an unrecognized character format. Below is the code #include #include void db_sp(FILE*); int…
asdf
  • 375
  • 2
  • 4
  • 14
0
votes
3 answers

Read reports Bad File Descriptor despite getc successfully using the same fd to read a char

I have this C code: FILE * fd = fopen(filename,"rb"); printf("%c ",(char)getc(fd)); // returns expected char unsigned char buffer[10]; printf("%d ",read(fd, &buffer, 10)); // -1 printf("%d\n",errno); // 9 getc returns a char from the input file, as…
0
votes
1 answer

Cygwin: missing stream data due to stdio putc + line buffering

The expected output from the following program is: received REQUEST from client received REPLY from server this is seen on, for instance, GNU/Linux. However, on Cygwin the actual output is this: received REQUEST from client received from…
Kaz
  • 55,781
  • 9
  • 100
  • 149
0
votes
2 answers

Allocating memory dynamically without knowing the size of the string which is going to be entered

Below is the function which returns a character pointer to a string which was initialized using getc(stdin)- character by character. Is there any flaw in memory allocation method? Is this an efficient way when we don't know the size of the string…
Tharun
  • 425
  • 5
  • 13
0
votes
1 answer

getc() for passed in input and file reading in C

I have to develop a program in C that can have two kinds of inputs. By feeding it a string ( I am assuming like this filename < String1234455678, please correct me if I am wrong). By reading data from some file(s). I have to do some checks…
Cesar A
  • 663
  • 1
  • 7
  • 10
0
votes
1 answer

Segmentation fault in getc

Its just a program where i am trying to read the number of occurrences of the word passed as an argument in the file which is also passed as next argument. Code looks like below : #include extern void exit(int); void main(int argc, char…
bluefoggy
  • 961
  • 1
  • 9
  • 23
0
votes
2 answers

Cannot print characters from a file

I am trying to read a file character by character and print it on screen. However, the character is not displaying, I am getting a box with 0001 in it. This is my code #include #include int main() { FILE *fp; int ch; …
user3531263er
  • 423
  • 1
  • 4
  • 20
0
votes
1 answer

EOF character is not detected when typing line of text first?

char ch; while((ch=getc(stdin))!=EOF) { putc(ch,stdout); } As we know that EOF character can be inputted by ctrl-z. I ran the program two times:- 1- When I input ctrl-z, the loop gets terminated, which is acceptable. 2- When I input…
kevin gomes
  • 1,775
  • 5
  • 22
  • 30
0
votes
3 answers

Why does my getc() always return 30 when input is 0 in C?

int main(void){ char buffer[5] = {0}; int i; FILE *fp = fopen("haha.txt", "r"); if (fp == NULL) { perror("Failed to open file \"mhaha\""); return EXIT_FAILURE; } for (i = 0; i < 5; i++) { int rc =…
Pig
  • 2,002
  • 5
  • 26
  • 42