300 questions
1
vote
1
answer
72
views
VS Code. File contents are not displayed in TERMINAL and OUTPUT
I'm using VSCode on Ubuntu 24.04.01.The screenshot shows a program that reads and displays the contents of a file. The "my_file.txt" contains the word "Hello". When I run the code ...
0
votes
1
answer
49
views
function to return number of characters in n line of a file return always those of line 2 in c
the function is supposed to return the length of a line specified by the 2nd argument
but the result is either 0 or the one in the 2nd line whatever n may be:
int line_length(const char *filename, int ...
0
votes
1
answer
89
views
Why does this code print null characters and why does it find null characters in both files? [closed]
bool isIdentical(FILE *file1, FILE *file2) //this fun tell me files is identical or no
{
// this must have char1 in file1?
char c1 = fgetc(file1);
// this must have char2 in file2?
...
0
votes
1
answer
63
views
fgetc reading file in C
the fgetc function in C should read just on caracter but in this code read more then one caracter .
in this code i test the function fseek to move the cursur and get each time the next caracter using ...
0
votes
0
answers
70
views
About fgetc() in C
The code paragraph is as follow:
fp_t=fopen(temp_name,"r");
int j=0;
do
{
char ch=fgetc(fp_t);
if(feof(fp_t)==0) all[i].note[j]=ch;
j++;
} while(feof(fp_t)==0);
all[i].note[j-2]=...
3
votes
5
answers
174
views
Does a character have to be casted to unsigned char before being compared to getc family returns?
I am not sure about whether I have to cast a character to an unsigned char before being compared to the return of a getc family function.
The functions I consider getc family, are getc, fgetc and ...
1
vote
3
answers
114
views
I'm trying to read text from a .txt file and count the amount of words in it using c
This is what I have so far. The issue is that the "count" variable is not properly adding to itself when it hits a newline or space character.
#include <stdio.h>
#include <stdlib.h&...
0
votes
2
answers
98
views
fgets() waiting input for file write [duplicate]
I'm working on my code. If I use scanf instead fgets, It works but I want to use fgets. If I use fgets after I enter number of lines, I need to press enter. How can I fix this problem? Is this normal? ...
-1
votes
1
answer
99
views
`fseek` issue when printing last $N$ lines to a file using C++
I'm using the following code to print last N lines of one file to the other.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void printLastNLines(...
1
vote
0
answers
33
views
Why do I need to add one more fseek() function in my code after fgetc() function? [duplicate]
In my problem I was using fseek() function to read a character from the 4th position. After that I wanted to write a new character after that position where the file pointer is pointing to. As after ...
0
votes
0
answers
117
views
C program inside docker container ignores fgetc(stdin)
I have a C program running inside a Docker container and calling a function getkey() as below:
int getkey() {
int ch;
struct termios orig_term_attr, new_term_attr;
/* set terminal to raw mode */
...
0
votes
1
answer
164
views
Unable to print the character 'à' with the printf function in C
I would like to understand why I can print the character 'à' with the functions fopen and fgetc when I read a .txt file but I can't assign it to a char variable and print it with the printf function.
...
1
vote
1
answer
139
views
Storing mac addresses and device names listed in a text file into a struct array using C
I am trying to take a list of MAC Addresses and Device names from a text file and store them in an array called list;
struct list {
char ch;
char a[2], b[2], c[2], d[2], e[2], f[2], g[2], ...
0
votes
2
answers
60
views
why does only the 1st file reading function executes over multiple programs of the same kind in C language?
This code contains 3 file handling related functions which read from a file named "mno". But only the 1st called function in the main() is working. If the 1st function of the list is ...
0
votes
0
answers
47
views
Fseek a txt to print by character using fgetc
fseek(exsist, start, end);
while(count < end)
{
if(feof(exsist)) break;
c = fgetc(exsist);
printf("%c",c);
if(feof(exsist)) break;
++count;
...
0
votes
0
answers
47
views
Odd segfault occuring on EOF exit of fgetc while loop
So I've combed through this thing pretty thoroughly using printf statements (on mobile) and have narrowed the location of the error down to the nested if (ret == EOF) statement in the read_file ...
0
votes
2
answers
187
views
Filling the \0 spaces of an array with 'x'
I'm new to C programming so if anything looks super wrong I'm sorry!
I need to make my array ( str[512] ) to be exactly 512 characters long after taking input from a text file with an unknown number ...
1
vote
2
answers
129
views
File reading with fgetc in C
I have a simple file reading algorithm but it's not returning the values that are in the file. The below is the code. The input txt values are 200 53 65 98 183 37 122 14 124 65 67 but the code is ...
0
votes
1
answer
108
views
Making myfgets using char pointer as an input
char *myfgets(char *s, int n, FILE *in) {
char ch;
if (n<=0) {
return s;
}
while (((ch = getc(in)) != '\n')) {
if(ch == EOF){
return NULL;
}
...
0
votes
2
answers
985
views
How to skip a particular line from a text file if that line has white space or a particular character in C programming?
I'm new to string and file operations in C and I have a text file file.txt with below data.
I'm reading a text file which contains below information
1.#comment
2.Name="Audi"
3.Class="...
-1
votes
1
answer
770
views
Why does fgetc() in C always reads extra, non-existent characters whenever I try to read non-printable characters from txt files?
I am trying to read non-printable characters from a text file, print out the characters' ASCII code, and finally write these non-printable characters into an output file.
However, I have noticed that ...
1
vote
2
answers
1k
views
fgetc vs getline or fgets - which is most flexible
I am reading data from a regular file and I was wondering which would allow for the most flexibility.
I have found that both fgets and getline both read in a line (one with a maximum number of ...
8
votes
3
answers
2k
views
What are all the reasons `fgetc()` might return `EOF`?
Certainly fgetc() returns EOF when end-of-file or an input error occurs.
Is that all and does that mean no more data is available?
FILE *inf = ...;
int ch;
while ((ch = fgetc(inf)) != EOF) {
;
}
if (...
1
vote
2
answers
200
views
how to print line numbers from multiple files without using fgets
I'm trying to print line numbers in the beginning of the lines without using fgets()
yes, it prints line number well when I input multiple files
but I want to get result like this. Can you guys help ...
0
votes
0
answers
36
views
Sorting word C language from a .txt file: \n "space" undectable
I'm trying to code a little program to sort words frome a .txt file.
I use Dev-C++ 5.11 as IDE with GCC 4.9.2.
Below, the .txt file content:
aaa ert ert ert
zzzz
aaa
zzzz
#
ert
ert
#xxx
I have to ...
0
votes
0
answers
226
views
Using ungetc() to get the length of stdin for allocate memory, is it possible?
Here is the logic what I am trying to do;
get input char with fgetc() in the variable named ch,
check if not ch is EOF or equal to '\n',
increase int variable named counter,
then put the variable ch ...
0
votes
1
answer
1k
views
Read integers AND characters from file line by line in C
I have a txt file of this form:
11
10
BU
1U
0U
0U
...
I would like to read each digit/character one by one from the file in C.
The first two rows contain 2 integers, the rest of the rows contain ...
1
vote
1
answer
294
views
How to load 2D char array from file and print it with mvprintw() in C using ncurses.h?
I'm coding a small game and i decided to load a map from the file but I'm facing a problem.The file looks like this :
and i need to print it like it is in the file expect of dots. They has to be ...
1
vote
1
answer
1k
views
Read and save each word of a txt file using C?
I'm trying to write a program that :
open a txt files
read the first word of a line and store it in a variable (or an array) => it'll be a name
read the second word of a line and store it in a var/...
0
votes
1
answer
366
views
How to use fgets after using fgetc?
I'm trying to write a specific program that reads data from a file but I realized that when I read the file with fgetc, if I use fgets later, it doesn't have any output.
For example, this code:
#...
0
votes
0
answers
40
views
Why is the rest of my code is being ignored after I only have sent the first input?
I'm trying to write a program that asks for input of the name of a file and a char to be counted inside the file. But whenever I input the proper name of a file (like, "file.txt") it jumps ...
0
votes
2
answers
95
views
how to use the return type of "fgetc" blow the code?
#include <stdio.h>
int main ()
{
FILE *fp;
int c;
fp = fopen("file.txt","r");
while(1)
{
c = fgetc(fp);
if( feof(fp) )
{
break ;
...
2
votes
1
answer
307
views
Using fscanf *after* fgetc issue
In C, there are many posts concerning using fgetc after fscanf, dealing with an additional \n, but I am seeing another issue when using them in the reverse order; fscanf after fgetc.
When using fscanf ...
0
votes
1
answer
408
views
fgetc only reads UTF8 encoded file. not working for UTF16
My aim is to find the encoding of a text file by dividing the size of the file by the number of characters in the file. but fgetc only reads UTF8 encoded files. not working for UTF16. Kindly help me ...
0
votes
1
answer
811
views
Check if file contains content from another file C
So I'm working on a project, and I need to check whether or not a certain file contains the contents of another file.
I made a function in c (It's c not c++) to try and to it but it seems to get into ...
0
votes
2
answers
259
views
Filling an array of struct from a file with fscanf while checking new line (feedback)
I have a .txt file where every line is like:
id name surname 78 99 101 12 33 44
Every line I need to fill a struct of Student. The numbers after surname have to be stored in an array of structs ...
1
vote
2
answers
406
views
My fgetc is not returning a desired output. (C programming beginner)
My .txt file I have created before running the code looks like the following:
/*
I am Jason and I love horses hahaha/n
This is amazing, How much I love horses, I think I am in love/n
how many people ...
0
votes
0
answers
241
views
How to read a binary file from the end to the start in c?
I have tried to read a binary file from end using fseek() to move the cursor to the end and fgetc() to read the file.
fseek(fptr, 0, SEEK_END);
int size = ftell(fptr);
unsigned char c;
int i =0;
...
1
vote
1
answer
979
views
Why does fputc() append to my file and not overwrite it?
I'm writing a C Program that changes all uppercase characters to lowercase from a certain file. The problem is, after storing all characters (processed) in a string, and trying to print all characters ...
0
votes
1
answer
133
views
unable to store read characters in a string (C++)
I am trying to read text in a file and store it in a string so that it can be output and read
although when I try to see the output string it seems to have a completely different output.
does anyone ...
1
vote
1
answer
544
views
fgetc() adds carriage return to line feed
I'm trying to make a program that concatenates multiple files to one. The code that i currently have is the following:
#include <string.h>
#include <stdio.h>
void main(int n, char** args) ...
3
votes
2
answers
670
views
C - Copying text from a file results in unknown characters being copied over as well
When running the following C file, copying the character to fgetc to my tmp pointer results in unknown characters being copied over for some reason. The characters received from fgetc() are the ...
1
vote
1
answer
126
views
For loop is not picking up fgetc
I'm trying to get the fgetc command working in a very simple for loop. While (pun intended) it works in a while loop it doesn't work in a for loop and I can't figure why.
It should just grab whatever ...
0
votes
0
answers
67
views
Problem in the change of line when reading files in C
I'm reading a .pgm file (like the lines below) with "fgetc(file)". When getting only the numbers with a loop in which I avoid the spaces in between (it seems it treats each space, no matter ...
0
votes
1
answer
256
views
How to avoid second order sql injection attack when using database data with fgetc( stdin) in c++
std::string sQuery;
char ch= NULL;
ch = fgetc(stdin);
if(ch != EOF)
{
sQuery += ch;
}
checkmarx is complaining about the code saying that fgetc element gets database data and element value flows ...
0
votes
3
answers
85
views
faster way to do simple tracking of line and column information than a case statement
I'm running into trouble where on a release build I'm losing about 7-8MB/s of throughput due to a simple case statement:
switch(ch) {
case '\t': // tab
m_column+=TabWidth; // tabwidth is ...
1
vote
2
answers
106
views
fgetc output varies when code is placed inside function and executed
First, let me say that I apologize for the lack of specificity in this title. There honestly was no good way to describe what I encountered in a single sentence to be honest, but I will try to ...
0
votes
2
answers
269
views
Pushing numbers from a text file into a link list
I am trying to push numbers from a text file into a linked list which might have multiple digit numbers in multiple lines. My output is a mess, only printing -47 multiple times. My main doubts are how ...
0
votes
1
answer
188
views
Is there a way to go to the beginning of a file and at a specified index with fgetc in C?
For my algorithm, I would like to backtrack after going through all the chars in a file back to specific index char. For example, I have a file with ABCDEF, I want to access in the order ABCDEF then ...
0
votes
0
answers
210
views
Create my own fgets() function using read(fd, buf, sz)
I am trying to build my own lineread method to read a file line by line using a filedes, a buffer and a size parameter. My reasoning is that I can read the file using 1 as my size to go through every ...