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!!!!!
Printable View
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?
> What have you done so far?
Well they posted it twice :p
http://cboard.cprogramming.com/cplus...r-problem.html
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.Quote:
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]);
i am compiling in turbo c++4.5
i m getting general protection exception
Have you tried what I said?
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 ?Quote:
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:
Quote:
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().