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

Read a file and print the first letter of each word

I need to read a file and then print the first letter of each word in lowercase and their position in the file. I made this code but it's not working and i don't know why. #include #include #include int…
João
  • 7
  • 2
0
votes
1 answer

How can I get only the first character entered by the user?

Hangman I'm using scanf to get a character entered by the user (guessed letter), but it seems like scanf gets all the letters entered char guessed_letter; printf("\n"); scanf(" %c", &guessed_letter); enter image…
0
votes
0 answers

getc() behaviour in C cli application enter

this is my simple code: while (1) { /* code */ putchar(getchar()); } I just want to get simple symbol echo in terminal and wait output like this: aabbnneett, but getchar accept input only after pressing "enter"…
Artem
  • 1
0
votes
3 answers

Is my understanding of EOF (end of file) in C correct?

Lets say we have this piece of code: while ((ch = getc(fp)) != EOF) { ... } This is how i would assume the loop would work, please tell me if it is correct or not/if any mistakes have been made: getc reads character from I/O stream fp getc…
0
votes
1 answer

Understanding struct pointer in C EDIT: Improper use of feof()

I am having difficulty understanding why I am receiving an error with my C program. The main function makes a call to readFile() function which copies the contents of a text file to a 'Text' struct's 2D char array, then returns the struct. When I…
0
votes
2 answers

C strcat inserts garbage into string

My method reads an input text of vectors with the following format: 57.0000,-7.4703,-0.3561 81.0000,-4.6478,7.9474 69.0000,-8.3768,0.4391 18.0000,-4.9377,9.9903 62.0000,-5.8751,-6.6054 ... My attempt to read each vector and insert it to an array is…
Kahalon
  • 119
  • 1
  • 12
0
votes
1 answer

Trouble reading file into 2d array with getc

Evening, I'm having trouble getting a file to read into a 2d array that I've allocated (maze[][]). The 2d array is allocated and sized according to the max rows (number of lines) and max columns (line with longest number characters). I'm using getc,…
0
votes
3 answers

In C, "getc" reads only three lines from text file

My Code is HERE int main(){ FILE *fp; fp = fopen("dic.txt", "r"); while(getc(fp) != EOF){ if(getc(fp) == ' '){ printf("up "); } } } My dic.txt is HERE dic.txt my predict is that "up up up up " because, there are four space "…
skk
  • 67
  • 4
0
votes
1 answer

Reading file of character data with while loop

I have a file with data like: 1F B8 08 08 00 00 00 00 I am reading it into an int, casting it to a character, and storing it in an array. int n, i = 0; char c; while (ifs >> std::hex >> n) { c = static_cast(n); r[i++] =…
0
votes
1 answer

How to make a Hexadecimal value input from the console for getc

As we know, if we code a string like "\x61\x61" in a c source file, it actually means "aa". When inputting a char from the console for the function of getc or fgetc, is there anyway that we just give some Hex value? Maybe something like '\x61' but…
user3236879
  • 511
  • 2
  • 7
  • 17
0
votes
2 answers

puts() doesn't flush the buffer in io redirection program

the code as follows: int main(int argc, char **argv) { const char *file = "/tmp/out"; int fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); int stdout_tmp = dup(1); close(1); dup(fd); puts("hello world!"); //…
Harrison Lee
  • 55
  • 1
  • 6
0
votes
1 answer

Can we use two different methods to read a file in a same block of code?

// Method I`m using to read words,Characters,whitespaces,new lines from a file.. // But when i run it only the upper while gets executed and if i place the below whileloop first, //only that gets executed. In short only one while loop gets…
0
votes
2 answers

How to read a file and use getc in C?

I am a senior Comp Sci major about to enter my last semester where I will be taking just one class involving only the language C. I was attempting to practice my skills by making a rather simple program that I conceived of. I simply want to read a…
PluffTed
  • 53
  • 5
0
votes
1 answer

All C functions to read a file sets EOF even before reaching SEEK_END

I'm writing a C program to share files between 2 computers (running Windows OS) connected to a common network (Just a lab project). When I read non .txt files like .docx or .gif I couldn't get the entire file. Most of the time, the server program…
San
  • 453
  • 3
  • 14
0
votes
2 answers

Why can't use "getc(f)) != EOF" to compare directly?

I'm new to C language, and now get stuck with this kind of question: why do I get a weird result if I use above expression to print string in file? Here is the situation: I have a file(data.txt) with the following content: "Hello Everyone!!" And…
Bcpp
  • 25
  • 3