char text[1024];
fgets(text, 1024, stdin); //Foydanaluvchi kiritadi matn
FILE * file = fopen("my.txt", "w"); //yozish uchun file degan fayl ochiladi
for(int i=0; i<strlen(text); i ){
if(text[i] == 46){
fseek(file, 0, SEEK_CUR);
fputc(text[i]='\n', file);
}else{
fputc(text[i 1], file);
}
}
fclose(file);
CodePudding user response:
Your logic does not make sense. Work it out on paper with a simple example to see that
here:
fputc(text[i]='\n', file);you are writing a newline instead of the period, not after it; and
here:
fputc(text[i 1], file);you are writing a different character than the one you just tested. Moreover,
this:
fseek(file, 0, SEEK_CUR);asks to adjust the file position to its own current value. There may be circumstances where that's actually useful, but yours are not among them.
I think you're making it harder than it should be. All you really need to do to implement the behavior described is loop through the data, write each character, and if that character was a period ('.') then additionally write a newline.
Although the question does not say so, possibly you are expected to avoid adding extra newlines where there already is one after a sentence. That would require looking ahead at least one character, but it is doable.
You might also consider reading the file one character at a time (with getchar(), say). That would actually make your logic a little simpler, and it might smooth out some technical details if you have to avoid adding duplicate newlines, or if you need some other extra behavior such as eliminating space characters between sentences.
CodePudding user response:
It is difficult to modify the file in place as you are trying to do, because there is no standard function or system call to insert bytes in the middle of a file.
The recommended approach it to write a simple filter that reads the text from stdin and writes the modified contents to stdout:
#include <stdio.h>
int main() {
int c;
while ((c = getchar()) != EOF) {
putchar(c);
if (c == '.')
putchar('\n');
}
return 0;
}
Note however that the above code will add a newline after each ., some of which might not end a sentence, eg: I paid $3.49 for each bug and some of which may already be followed by a newline, thereby doubling them.
To avoid both of the above pitfalls, you might only output the newline if the . is followed by a space. In this case, you can replace the space after each . with a newline, so you could operate in place on systems where newline is a single byte:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *fp;
int c, last = 0, count = 0;
if (argc < 2) {
fprintf(stderr, "missing file name\n");
return 1;
}
if ((fp = fopen(argv[1], "rb ")) == NULL) {
fprintf(stderr, "cannot open file %s: %s\n", argv[1], strerror(errno));
return 1;
}
while ((c = getc(fp)) != EOF) {
if (c == ' ' && last == '.') {
// call fseek to move back one byte
fseek(fp, -1L, SEEK_CUR);
// overwrite the space with a newline
putc('\n', fp);
// call fseek to allow switching back to read mode
fseek(fp, 0L, SEEK_CUR);
count ;
}
last = c;
}
fclose(fp);
printf("%d changes\n", count);
return 0;
}
