Questions tagged [fgetc]

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

Anything related to C or C++ standard library functions fgetc (defined in <stdio.h> C standard header) or std::fgetc (defined in <cstdio> C++ standard header). These functions are used to read a single character from a stream.

See CPPreference.com:

294 questions
0
votes
2 answers

Troubles reading after end of line in C

So, I'm having trouble reading from my file. I want to read this file ; 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 1 0 0 0 0 0 1 2 2 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The first 1 is my player, while the…
0
votes
3 answers

Number of characters in a file?

I would like to know if there is a method for reading characters from a file and saving them into an array without a specified array length. In normal situation I read all the characters and count them. (step 1) Then I create the array with malloc…
MaXiMkA
  • 449
  • 3
  • 6
  • 14
0
votes
3 answers

fgetc to skip from point to new line

I am trying to get fgetc to read through a file and skip from a certain indicator until a new line. This seems like a simple question, but I can't find any documentation on it. Here is an example of my question: read this in ; skip from semicolon…
AlexK
  • 336
  • 8
  • 21
  • 41
0
votes
1 answer

fgetc not starting at beginning of large txt file

I am working in C and have a text file that is 617kb that I am trying to read with fgetc. For some reason fgetc is starting randomly within the file. I have tried moving the file pointer to get beginning with fseek with no success. I can get fgetc…
TinMan
  • 99
  • 2
  • 13
0
votes
3 answers

Read file to string

I would like to read a file to a string. I have the following code which can be compiled but cannot be run. My idea is to use a while loop to append every character in the file to the end of the string until EOF. char string[50000]; FILE…
Johnson
  • 31
  • 5
0
votes
2 answers

How to read a text file 3 characters at a time?

I'm learning to program in C, and right now my homework is like this. I am supposed to be able to take a text file with words/letters and my program should print the ascii codes+1 of the letters it gets. It's "encoding" it. So for example the letter…
JustAGuy
  • 13
  • 1
  • 6
0
votes
3 answers

fgetc not starting at beginning of file - c

Problem solved here: fgetc not starting at beginning of large txt file I am working in c and fgetc isn't getting chars from the beginning of the file. It seems to be starting somewhere randomly within the file after a \n. The goal of this function…
TinMan
  • 99
  • 2
  • 13
0
votes
1 answer

fgetc not working C- returns same char repeatedly

I am working in C and trying to read a file and save the chars returned by fgetc in an array. The problem is that fgetc is returning a random char repeatedly. Marked location in comment. For example, "wwwwwwwwwwww..." or "@@@@@@@@@@@@...". Any help…
TinMan
  • 99
  • 2
  • 13
0
votes
2 answers

Discrepancy with fgetc while reading a text file

I´m beginning with C and I´m willing to understand certain conditions. I have a text file, generated by notepad or direct via shell by echo in a windows os. When running this the output show extra chars. What I ´m doing wrong? How I can read text…
Guilherme Viebig
  • 6,901
  • 3
  • 28
  • 30
0
votes
2 answers

How to fix the loop?

I need a program to get the input from a file using fgetc. The first condition is to find the newline '\n' then copy all the characters. After that, if you encounter a comma ',' the program needs to stop copying. My code compiles but when I run it,…
Lloyd
  • 85
  • 1
  • 1
  • 11
0
votes
1 answer

Getting character from fgetc() in C

I have a character obtained from fgetc(file) stored in the variable i. I want to compare the character contained within i to a constant, i.e. (i == "\n"), but of course i is of the integer type. I tried ((char)i == "\n") but it doesn't work. In both…
Tyler
  • 2,579
  • 2
  • 22
  • 32
0
votes
2 answers

reading characters using fgetc

I have been trying to read a file using fgetc but i am unable to break the line into the pieces that i require ex if i have: x: xx xxx, xxxx; and i want to break that into: x xx xxx xxxx
user2785532
  • 53
  • 1
  • 6
0
votes
3 answers

Reading and Displaying content of the Text File via C coding

I am trying to code a game in C that deals with selection of races. Each races has their own "stories" and when the user chooses to read one of their stories, what I want to happen is, While the program is running on Command Prompt, it will display…
Sung Min Kim
  • 505
  • 2
  • 5
  • 11
0
votes
4 answers

C filling an array from a text file

I'm trying to fill an array from a text file. I'm using fgetc and my problem is dealing with the newline characters that are in the text file. I've currently got, for(i = 0; i < rows; i++){ for(j = 0; j < columns; j++){ if((fgetc(fp) ==…
jrandj
  • 175
  • 2
  • 4
  • 11
0
votes
2 answers

Infinite-loop when reading from a text file

I have read several questions on this site and managed to write the code below which reads data from a file (shown before the code, below). The problem is that the code gets in an infinite loop in the do {...} while segment, indicating that it does…
Alexis S.
  • 5
  • 5