+ 2
How can i print the 4 lines from a file to a program? (c language )
123 Respuestas
+ 5
#include <stdlib.h>
#include <stdio.h>
int main(){
int count = 0;
char c = 0;
FILE * fp = fopen("file.txt","r");
if(fp){
while(c != EOF && count < 4){
c = getc(fp);
if( c == '\n' )
++count;
printf("%c",c);
}
fclose(fp);
}else
printf("Failed to open the file\n");
return 0;
}
+ 4
No problem, I asked to be sure that you would not have problem on your interview :)
Error found !! Change type of i from char to int
+ 3
Depends of the structure of the file !
It is the same exercise as reading a string but you read each characters from a file instead
+ 3
Ok but I'll need more informations. How the student information is written in the file ?
+ 3
I know @Madhuri, I am helping you the best I can :)
If you did not already changed what @~ swim ~ and I said, do it or it will not work ! :/
+ 3
(a chat would have been useful in this case ...)
+ 3
I'll correct your code and post it here
+ 2
OK. thank you
+ 2
thank you Baptiste
+ 2
No, I know that, it is not what I mean. The path in my program is not the same as yours, so the first problem is that you will see no change in c:\\file.rec as I modify ./file.rec
Next is I changed the way to save/load from text to binary so if you open the file created, you will see something that has no sense while the data is here with no problem ! (as you cannot see the content of a binary file with a text editor)
Last is that you'll need to delete your current c:\\file.rec as it is a text file and not a binary file (don't worry, the program create it if it was not already)
Are binary files allowed for your interview ? I hope so, they are cleaner and safer ^^
+ 2
we are really tankfull to you.baptiste.thank you so much..
+ 2
thank you so much baptiste it worked
+ 1
This is what I would do to read/save the file :
save :
FILE * fp = fopen("file.bin","wb");
if(fp){
fwrite(s1,sizeof(struct student),100,fp);
fclose(fp);
}
read :
same thing with two changes
wb -> rb
fwrite -> fread
It is a binary saving, but you cannot save pointers with this as the address will be freed at the end of the program. If you want to save a pointer, you'll have to save what it stores on saving and reallocate it on reading
+ 1
You can, I never said you couldn't ^^
If you read the file and saved its informations in a student array, then it is way easier to manipulate than re reading the file each time.
+ 1
It is normal, you can't run proper C on this platform ! I asked to tell me what was not working because I cannot go to my PC for now :/
+ 1
Ok.... Lemme chk out
+ 1
It is not in the good file as I changed the path for my computer (I am on linux)
The other point is that you will not be able to read it as it is a binary file, but your program still can read it
Sorry, I do not know what you mean by "storing in file only" :/
+ 1
we are doing in windows codeblocks.
if i give the path it will store in that path and we cam access it
+ 1
Actually its not mandatory for an interview...... If we make this student data base project it... I wld be definitely helpfull for us.... since mini projects give ur chances of getting selected is more