fgetc not starting at beginning of large txt file - c

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 work to fine with smaller files. Any help is appreciated.
Sample input is 25,000 lines of data similar to:
Product
23 660
2366 3
237 09
2 3730
23734
23 773
241 46
Source:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void print(FILE *category){
int ch = 'a';
while ((ch = fgetc(category)) != EOF){
printf("%c", ch);
}
getchar();
}
int main(void){
FILE *category = fopen("myFile", "r");
if (category == NULL){
puts("category file not found");
}
else{
print(category);
fclose(category);
return 0;
}
}

I suspect the problem lies elsewhere.
Where is the output from this program going? If you're sending it to the console, its scrollback buffer won't be large enough to contain the whole file. Maybe it just looks like fgetc() is starting in an odd place.
Try diverting the output from this program to a new text file and compare the size of this file with the size of the input file, e.g.:
./category_print >output.txt

Related

C - Adjusting my program to read a file line-by-line without setting a maximum length on each line with fscanf

I'm learning C and how to read/write based on file contents, but I'm having some difficulty.
Currently in this test program, I'm attempting to read from the file test.txt line-by-line without specifying the max length of the line, so basically I can't use fgets.
Instead, I try to use fscanf. But 1) I still have to specify an overbound for the length of the char array and 2) The program doesn't even work -- all it reads is the very first letter of the first line.
What am I doing wrong and how can I change the program so I don't have a max bound on length of each line (in this example its 1000)?
#include <stdio.h>
#include <stdlib.h> // For exit() function
int main() {
char c[1000];
FILE *test;
if ((test = fopen("test.txt", "r")) == NULL) {
printf("Error! Couldn't find file");
exit(1);
}
fscanf(test, "%[^\n]", c);
printf("Data from the file:\n%s", c);
fclose(test);
return 0;
}
for reference here's my current test.txt:
abcd
efgh
ijkl
mnop
qrst
uvwx yz

How to move the position pointer to next line in the file using fseek

I have seen programs for file handling and in one of the program using fseek as shown below:
/* This example opens a file myfile.dat for reading.
After performing input operations (not shown), it moves the file
pointer to the beginning of the file.
*/
#include <stdio.h>
int main(void)
{
FILE *stream;
int result;
if (stream = fopen("myfile.dat", "r"))
{ /* successful */
if (fseek(stream, 0L, SEEK_SET)); /* moves pointer to */
/* the beginning of the file */
{ /* if not equal to 0
then error ... */
}
else {
/* fseek() successful */
}
}
Like this can one move the file pointer to the next line immediately after that line
BO_ 377 FC_DM_MISC: 8 FC
SG_ DATA3 m11 : 31|8#0+ (1,0) [0|0] "" DM
These are the two lines and I want to program in a way that when one identifies the number 377 the pointer should now go to the next line i.e., to the line SG_ DATA3 inspite of the white spaces after 8 FC. How can one do that using fseek in C?
Try this code . It may help you .Here the Each line of the Input file is converted to string ,since string manipulation is very simple comparing to complex fseek() function.This may not be perfect answer but this will be very simple solution.
#include <stdio.h>
#include <string.h>
int main(void)
{
FILE *stream;
int result;
char tmp[100]; // assuming that max length of a line in myfile.dat is 100.
if (stream = fopen("myfile.dat", "r"))
{ /* successful */
fscanf(stream, "%100[^\n]", tmp); // assuming that max length of a line in myfile.dat is 100.
printf("%s", tmp);
if (strstr(tmp, "377"))
{ // check for 337
fscanf(stream, "%100[^\n]", tmp); // next line is in the string tmp .
// continue your program.
//printf("%s", tmp);
}
}
}
fseek is used for binary data, if you work on a text file you should use either fgets or getline(recommended to use getline).
There's an open discussion of "fgets() vs getline" and many say that "fgets is deprecated" is only a gcc propaganda in favor to their specific getline().
A possible flaw in fgets() is that it doesn't tell you anything if there are null bytes being read, something you can get away with getline().
But then again if you don't like gcc, or use something different, use fgets(). If you are stuck with gcc, then use getline().

How to read text file in C?

I'm trying to read a txt file containing strings of 1s and 0s and print it out in the manner below. I tried my code a couple of months ago and it worked fine in reading the text file. Now when I tried it, it outputs something really strange. Also I tried changing the directory of the file to a non-existant file but it still outputs the same thing when it should've quit the program immediately. Please help!
The content of txt file:-
10000001
01110111
01111111
01111010
01111010
01110111
Expected output:-
data_in<=24'b10000001;
#10000;
Real output:-
data_in<=24'b(some weird symbol that changes everytime I recompile);
#10000;
My code:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
int i, j;
j = 0;
char words[50];
FILE *fp;
fp = fopen (argv[1], "r");
if (fp == NULL) {
printf ("Can't open file\n");
}
while (feof (fp) == 0) {
fscanf (fp, "%s", words);
printf ("data_in<=24'b%s\n", words);
printf ("#10000\n");
}
fclose (fp);
system ("PAUSE");
return 0;
}
The input argument is the following:-
"C:\Users\Beanz\Documents\MATLAB\football frame\frame1.txt"
Read each line one by one with getline(3) -if available- or with fgets (you'll then need a large enough line buffer, at least 256 bytes), then parse each line buffer appropriately, using sscanf (the %n might be useful, and you should test the scanned item count result of sscanf) or other functions (e.g. strtok, strtol, etc...)
Remember that 'feof()' is only set AFTER trying to read PAST the end of the file, not when at the end of the file.
So the final iteration through the loop will try to read/process data that contains trash or prior contents.
Always check the returned value from 'fscanf()' before trying to use the associated data.
strongly suggest
eliminate the call to feof() and use the fscanf() to control the loop

basic file input using C

So im working on learning how to do file I/O, but the book I'm using is terrible at teaching how to receive input from a file. Below is is their example of how to receive input from a file, but it doesn't work. I have copied it word for word, and it should loop through a list of names until it reaches the end of the file( or so they say in the book), but it doesn't. In fact if I leave the while loop in there, it doesn't print anything.
#include <stdio.h>
#include <conio.h>
#define MAX 250
int main()
{
char name[MAX];
FILE*pRead;
pRead=fopen("test.txt", "r");
if (pRead==NULL)
{
printf("file cannot be opened");
}else
printf("contents of test.txt");
while(fgets(name,sizeof(name),pRead)!=NULL){
{
printf("%s\n",name);
fscanf(pRead, "%s", name);
}
getch();
}
Even online, every beginners tutorial I see does some variation of this, but I can't seem to get it to work even a little bit.
I believe your array is too small and therefore when you are reading fscanf overwrites memory causing bizarre behavior
If you just want to read the file - presuming now there is one name per line followed by newline in the input file - just read the file using fgets() instead.
#define MAXLINE 256
char name[MAXLINE];
while (fgets(name,sizeof(name),pRead)!=NULL)
{
// do whatever
}

File Handling in C

Sorry for asking very simple question.I'm new to coding.
Input txt file
5,3
001
110
111
110
001
I need to print the output as
U1: 001
U2: 110
U3: 111
U4: 110
U5: 001
Upto now I was able to print the contents with this:
#include <stdio.h>
void main() {
FILE *fopen(), *fp;
int c;
fp = fopen("read.txt","r");
c = getc(fp) ;
while (c!= EOF) {
putchar(c);
c = getc(fp);
}
fclose(fp);
}
Can Anybody tell how should I proceed?
Most of your work is done inside the while loop.
But you're not doing enough work ...
a) you need to count lines
b) you need to print the "U#"
Suggestion: create a variable for the line counting business and rewrite your loop to consider it. Here's a few snippets
int linecount = 0;
printf("U%d: ", linecount)
if (c == '\n') linecount += 1;
Oh! You really shouldn't add the prototype for fopen yourself. It is already specified with #include <stdio.h>.
And well done for declaring c as int. Many people do the error of declaring it char which is incompatible with EOF and all the range of characters
I would read the data a line at a time with fgets. When you have a complete line in a buffer, it'll be pretty easy to get fprintf to put in your line header with a format like "U%d: %s\n".

Resources