Reading specific lines using read function [duplicate] - c

This question already has answers here:
C Programming - Read specific line from text file
(2 answers)
Closed 5 years ago.
Assume I have a binary file data.bin of 1000 lines that was written using fwrite. For reading it, I just have to do something like this(where data is a double buffer of size 1000):
FILE *fp = fopen("data.bin", "rb");
fread(data, sizeof(double), (1000*sizeof(double),fp);
This will read the entire file but I am looking for reading only the last 500 lines!! This means I have to jump to the line 499 in data.bin and start reading from there until the end. How to do modify the previous fread function to read the last 500 lines?
Thank you.

Are you aware of the fseek() function. You need to set the pointer to begin at the last 500 lines then read until eof.
Look up fseek() and you will be able to solve this problem.
Here is a link for further information on fseek()

Related

I can't read and display txt file [duplicate]

This question already has answers here:
Why is “while( !feof(file) )” always wrong?
(5 answers)
Closed last month.
The community is reviewing whether to reopen this question as of 22 days ago.
I'm trying to read a file from txt docs and display the data, but when I run the code, it displays nothing and just shows like this:
enter image description here
I'm not sure where the problem is with my code, but perhaps the problem is because I need to display 4000 columns of data so the program cannot read and display it. I tried to use another sample txt with 10 columns of data and the code worked but not with 4000 columns of data.
This is my code :
char str [101][101];
FILE *fin = fopen ("test.txt", "r");
int count = 0;
while (!feof(fin)){
fscanf (fin, "%s", str[count]);
count++;
}
fclose(fin);
for (int i = 0 ; i < count ; i++){
printf("%s\n", str[i]);
}
This is data I want to read and display (4000 columns data)
Bali
Paris
Japan
Nepal
India
Rusia
Malaysia
Thailand
England
etc....
Thank you in advance!!
I haven't tried to run the code.
You read a file line by line and put every line within str. However, str can only contain a maximum of 101 lines, with each iteration not exceeding 100 chars.
Increase the value of str to at least 4000.
str[4500][101]
EDIT:
The question has been flagged as duplicate; however, the problem here is linked to how the C language works.
In this example, we have a static two-dimensional array for which C reserved some memory on the stack. As C tries to use more memory than allocated, the O.S. will inform the process that it does not have access to the location. Which makes C raise an exception at runtime.
I think bringing this precision to those who learn C is essential to avoid confusion.

Strange behaviour of fread() [duplicate]

This question already has answers here:
Read and write to binary files in C?
(7 answers)
what's the differences between r and rb in fopen
(5 answers)
Closed 4 years ago.
This is my code:
FILE* fLeftResult = fopen("C:/Users/Vincenzo/Desktop/unina/SOC/progetto esame/elaborazione fir/ResultLowLeft.bin","r+");
short output;
short matlabIdeal[SAMPLES+1] = {0};
size_t returnValue= fread(matlabIdeal, sizeof(short), SAMPLES, fLeftResult);
When SAMPLES is 6077, the array matlabIdeal is filled until the 4095th value. The successive values are 0. And returnValue is 1433.
When SAMPLES is 60772, the array matlabIdeal is filled until the 59391th value. The successive values are 0. And returnValue is 1433.
When SAMPLES is 30772, the array matlabIdeal is filled until the 30719th value. The successive values are 0. And returnValue is 1433.
The values that fread() fills are correct, but suddenly they became 0.
This is the binary file fread reads: https://ufile.io/sf85m
Can you help me with this problem? Or reproduce the code on your computer to see what will happen?
This is because you did not open the file for reading binary data.
fread is treating the stream as though it is text.
You should open the file using the "rb" mode.

C - fseek in combination with fwrite doesn't overwrite data [duplicate]

This question already has answers here:
What is the difference between "rb+" and "ab" in fopen()?
(1 answer)
Using fseek() with append
(1 answer)
Closed 5 years ago.
I have an array called dicevalues which contains 100 random int values from 1 to 6. It is newly randomly generated everytime I run the program.
I make each value into a char, so I can write it properly into a file. I want to set the pointer of the file to the beginning, so if I run the program again it overwrites the old data. However this doesn't seem to work. Everytime I run the code it only appends. Heck, no matter what values I set fseek to it only appends without overwriting anything.
fseek(fd,0,SEEK_SET);
for(i=0;i<100;i++){
dicechar = (char) (dicevalues[i]+48);//makes a char out of the int
fwrite(&dicechar,sizeof(char),sizeof(augenchar),fd);
};
What am I doing wrong?

reading from a text file in C Programming [duplicate]

This question already has answers here:
Reading a C file, read an extra line, why?
(6 answers)
Closed 8 years ago.
I am trying to write a C program that is able to read data (strings) from a text file and swap each content in terms of bytes. I have already implemented the code and everything works just fine, and and I am able to read all the contents of the specified text file, but the problem is that the program is printing the last word from the text file twice and I do not know why? Any help will be helpful ! This is the code I have:
while( !feof(ptr_file))
{
//to read in group of words (sentences) if
//needed !.
fscanf(ptr_file, "%s", userName);
//time to swap letters of the word coming from the text file.
swap_a_word(userName, 0, 4);
swap_a_word(userName, 1, 2);
//new space.
printf("\n");
//display the word after swapping to the screen for the user.
printf("%s", userName);
}
The program must not print extra data. I do not know, but when the program reaches the end of the file, it prints the last data of the text file twice. Please any hints will be helpful !.
Thanks !
The problem is the while loop condition
while(!feof(ptr_file))
Note that EOF is preceded by a newline '\n'. fscanf returns the value EOF when the end of file is reached. However, this does not set the end-of-file indicator on the stream and as a result the loop is entered one extra time. You should check the return value of fscanf instead to find number of items successfully matched and assigned.

c programming copying files

Having few issues with my copy program which creates a copy of a file user enteres. I decided not to use (size_t) structure instead just assigned (int) and (char) types variables so I know exact value of bytes to read() out. ie I know start at beggining of file and read 4 bytes(int) to get value of lenght of filename, which I use as size in next read()
So, when I am writing (copying file exactly with same name) users inputted file to the output file (copied file) I writing it in long string, without spaces obviously just to make it readable here,
filenamesize filename filecontentsize filecontent
ie 10 myfile.txt 5 hello
So when come to reading that data out I start at begining of file using lseek() and I know the first 4 bytes are (int) which is lenght of filename so I put that into value int namelen using the read function.
My problem is I want to use that value read for the filenamesize(first 4 bytes) to declare my array to store filename with the right lenght. How do I put this array into read() so the read stores value inside that char array specified, see below please
int namelen; //value read from first 4 bytes of file lenght of filename to go in nxt read()
char filename[namelen];
read(fd, filename[namelen], namelen);//filename should have 'myfile.txt' if user entered that filename
So my question is once I read that first 4 bytes from file giving me lenght of filename stored in namelen, I then want to read namelen amount of bytes to give me the filename of originally file so I can create copied file inside directory?
Thanks
int namelen; //value read from first 4 bytes of file lenght of filename to go in nxt read()
char* filename = new char[namelen+1];
read(fd, filename, namelen);
filename[namelen]=0; // Just to keep readed buffer c-string compatible
do something with filename
delete[] filename;
In your previous question here, you did not upvote a single answer or accept any of them. You do appear to have used those answers though.
People who answered that earlier question might be inclined to help you here if you could be bothered to show a little gratitude for their earlier help by upvoting their answers and accepting the one that you found most helpful.

Resources