Read the output of a file as
ABCDEFGHIFSFSA
1242487132
FSFSA
*
a) Print the output as
*
FSFSA
1242487132
ABCDEFGHIFSFSA
b) Print the output as
*
AFFSS
1122234478
AABCDEFFFGHISS
HELP NEEDED URGENT!!!!!
Read the output of a file as
ABCDEFGHIFSFSA
1242487132
FSFSA
*
a) Print the output as
*
FSFSA
1242487132
ABCDEFGHIFSFSA
b) Print the output as
*
AFFSS
1122234478
AABCDEFFFGHISS
HELP NEEDED URGENT!!!!!
What have you done so far?
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
> What have you done so far?
Well they posted it twice
FILE pointer problem
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Code:#include <stdio.h> #include <stdlib.h> #include <conio.h> int main() { FILE *fp1,*fpo; char s[20][100],ch; int i,j; fp1=fopen("pr31.c","w"); if(fp1==NULL) { printf("cannot open file"); } while(strlen(gets(s))>0) { fputs(s,fp1); fputs("\n",fp1); } printf("file closed\n\n"); fpo=fopen("pr32.c","w"); ch=fgetc(fp1); i=j=0; while(!feof(fp1)) { if(ch=='\n') { s[i][j]='\0'; i++; j=0; } else { s[i][j]=ch; j++; } ch=fgetc(fp1); } for(j=0;j<i;j++) printf("%c",s[j]); getch(); return 0; }
Are you sure ? it compiles for me.its not compiling......
whats the error message you are getting ?
my prog is writing to the file and reading from it the array of strings...
though it is compiling i am not getting the output correctly
s is declared as a 2D array, and while printing it you're using s[j] which will give you the corresponding addresses not the chars. You should be doing s[i][j]. Although I haven't read the whole code. There might be some other errors too.Code:printf("%c",s[j]);
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
i am compiling in turbo c++4.5
i m getting general protection exception
Have you tried what I said?
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
i need it urgent pls someone post me whole correct code
i am compiling in turbo c++4.5
i m getting general protection exception
=>BEN10
yes i did but still the general protection exception is occuring
Umm, you do know you actually have to close the file right ?printf("file closed\n\n");
Also, change your while loop to this and remove the fgetc at the bottom-Code:fclose(fp1);
Finally, you need to close your second file after your while loop-Code:while((ch=fgetc(fp1)) != EOF)Code:fclose(fpo);
Get back to your book and learn C properly. Why do you think it will work to read into a 2D array, for example?
The next step would be to burn Turbo C++ and throw it out the window.
Then get yourself a real IDE + compiler (try on here: SourceForge.net: Integrated Development Environment - cpwiki).
And then, NEVER EVER use gets: SourceForge.net: Gets - cpwiki
And finally, don't use feof as loop control; see faq.
[insert]In the wiki page it says size_t is the proper type for the lengths of the strings. I didnt get what it means by that. Is it some identifier in c which is predefined to hold the lengths of the strings?Code:At any rate, one can use something like this to replace gets(). #include <string.h> char * safegets(char * buffer, size_t count) { char * result = buffer; char * tail; if(buffer == NULL || count <= 0) result = NULL; else if(count == 1) *result = '\0'; else if(fgets(buffer, count, stdin) != NULL) { if( (tail = strchr(buffer, '\n')) != NULL) *tail = '\0'; } return result; } Note the return value of fgets() is checked. If fgets() returns NULL, it failed to read any input from stdin. This usually only happens if the user types End-Of-File, CTRL-Z on DOS and CTRL-D on UNIX, or the input was a file and the end of that file has been reached. Usually this can be ignored, but this function returns NULL if fgets() failed, just in case the caller wants to check it. As a side note, size_t is the proper type for the lengths of strings. Use fgets(), not gets().