-8

I am confused that how to give break line after every word which is present in a file.

Words in text file

Name    Date of birth     <----  I put this in code  
John    02\02\1999        <----  I want to jump to this line

I want this

Here is your: Name
Here is your: Date of Birth

But it is giving me this

Here is your: N 
Here is your: a 
Here is your: m 
Here is your: e 

And I don't know how to get it.

#include <stdio.h>
#include <stdlib.h>

int main(){

    FILE * fr = fopen("/home/bilal/Documents/file.txt","r");
    char ch;

    if(fr != NULL){
        while(!feof(fr)){
            ch = fgetc(fr);
            printf("Here is your %c\n: ", ch);
        }
        fclose(fr);
    }
    else{
        printf("Unable to read file.");
    }

    return 0;
}
  • `fscanf` with the `%s` format? – Some programmer dude May 14 '20 at 22:34
  • 5
    Also please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude May 14 '20 at 22:35
  • You're asking for a character at a time and that's what you're getting. – TomServo May 14 '20 at 22:47
  • how are words stored in your file ? – Hitokiri May 14 '20 at 22:49
  • I want word like name, date of birth etc which is present in text file. Not each character one by one. – Natasha Bibi May 14 '20 at 22:50
  • Horizontally stored 'Name Date of birth ID card number Phone number Address Account Fixing year Deposit amount' – Natasha Bibi May 14 '20 at 22:51
  • 1
    There are 3 words in `date of birth `. It seems that you want to print line by line, not word by word. – Hitokiri May 14 '20 at 22:57
  • So, you want to group together some words into a sentence, but if they are all on the same line, what should the program use to be able to tell where one sentence ends and the next one begins? The first letter of the word being in upper case signals the start of the next sentence? – isrnick May 14 '20 at 23:03
  • Yeah. Now I want a group of some words like 'Date of Birth' and then if it finishes then another like ID card number etc. The text file contains horizontal words not vertically. – Natasha Bibi May 14 '20 at 23:08
  • 1
    In the file is it written as `Date of birth` or as `Date of Birth`? If only the first letter in every word in all the sentences is in uppercase (like in `Date of birth`) it is possible to do something to separate sentences automatically, however the if that is not the case, such as in `Date of Birth` then there is no way to detect were one sentence ends and the next one begins, the only option would be to count the words while known previously how many words each sentence will have to group them together. – isrnick May 14 '20 at 23:19

2 Answers2

1

Within your while loop instead of immediately printing the character that you read, store the char in a char array. Add an if statement that does a comparison that checks if the read char is a space character. If it is you should print the stored array and set the index of the array back to 0.

Example:

#include <stdio.h>
#include <stdlib.h>

int main(){

    FILE * fr = fopen("file.txt","r");
    char ch[100];
    int index = 0;

    if(fr != NULL){
        while((ch[index] = fgetc(fr)) != EOF){
            //printf("%c\n", ch[index]);
            if(ch[index] == ' ') {
                ch[index] = '\0';
                printf("Here is your: %s\n", ch);
                index = 0;
            }
            else {
                index++;
            }
        }
        fclose(fr);
    }
    else{
        printf("Unable to read file.");
    }
    return 0;
}
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
1

Based on the line of text of the file provided we can assume that if the first letter of the word is in uppercase then it is the start of the next sentence:

Name Date of birth ID card number Phone number Address Account Fixing year

And use this to divide the line into sentences.

So here is the code by Christopher changed to group the words into sentences:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(){

    FILE * fr = fopen("file.txt","r");
    char ch[100];
    int index = 0;

    if(fr != NULL){
        while((ch[index] = fgetc(fr)) != EOF){
            if(index > 0 && ch[index-1] == ' ' && isupper(ch[index])) {
                ch[index-1] = '\0';
                printf("Here is your: %s\n", ch);
                ch[0] = ch[index];
                index = 1;
            }
            else {
                index++;
            }
        }
        ch[index] = '\0';
        printf("Here is your: %s\n", ch);
        fclose(fr);
    }
    else{
        printf("Unable to read file.");
    }
    return 0;
}
isrnick
  • 621
  • 5
  • 12