+ 2

How can i print the 4 lines from a file to a program? (c language )

22nd Aug 2017, 4:50 PM
Srinivas Rao H
Srinivas Rao H - avatar
124 ответов
+ 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; }
22nd Aug 2017, 5:51 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 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
27th Aug 2017, 1:59 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 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
24th Aug 2017, 5:24 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
Ok but I'll need more informations. How the student information is written in the file ?
24th Aug 2017, 5:26 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 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 ! :/
27th Aug 2017, 6:53 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
(a chat would have been useful in this case ...)
27th Aug 2017, 6:56 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
I'll correct your code and post it here
27th Aug 2017, 8:08 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
OK. thank you
24th Aug 2017, 7:02 AM
Madhuri Vishwamithra M.S
+ 2
thank you Baptiste
26th Aug 2017, 1:18 PM
Madhuri Vishwamithra M.S
+ 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 ^^
27th Aug 2017, 11:28 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
we are really tankfull to you.baptiste.thank you so much..
27th Aug 2017, 3:29 PM
Madhuri Vishwamithra M.S
+ 2
thank you so much baptiste it worked
27th Aug 2017, 4:16 PM
Madhuri Vishwamithra M.S
+ 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
24th Aug 2017, 6:19 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 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.
27th Aug 2017, 8:07 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
27th Aug 2017, 8:26 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 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 :/
27th Aug 2017, 8:40 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
Ok.... Lemme chk out
27th Aug 2017, 8:40 AM
Srinivas Rao H
Srinivas Rao H - avatar
+ 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" :/
27th Aug 2017, 11:21 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
we are doing in windows codeblocks. if i give the path it will store in that path and we cam access it
27th Aug 2017, 11:24 AM
Madhuri Vishwamithra M.S
+ 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
27th Aug 2017, 11:31 AM
Srinivas Rao H
Srinivas Rao H - avatar