Thread: Reading from a file and print in a different line

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    47

    Reading from a file and print in a different line

    So, this time I'm trying to read a text from a file, and print each word in a different line. Since I'm using my linux laptop atm and my debugger doesn't work, i have some troubles to see what's wrong.

    Let's say the text is this
    Code:
    This (2space) is(3space)
    just(4space) a
    test
    Here is my code
    Code:
    #include <stdio.h>
    #include "simpio.h"
    #include "string.h"
    
    #define M 30
    void read_input(FILE *input_file,char input_ar[]);
    
    int main(){
        char input_file_name[M],input_array[M];
        FILE* input_file;
    
    
        while(TRUE){
        printf("dwste to onoma gia to arxeio eisodou :");
        fgets(input_file_name,sizeof(input_file_name),stdin);
        input_file_name[strcspn(input_file_name,"\n")] = '\0';
        input_file = fopen(input_file_name,"r");
        if (input_file != NULL)
            break;
            printf("Cannot open input file %s. Try again .\n",input_file_name);
    
    }
    
    
    
    read_input(input_file,input_array);
    fclose(input_file);
    
    return 0;
    }
    
    
    void read_input(FILE *input_file,char input_ar[]){
        char ch = ' ';
    
        while (ch != EOF){
            ch = fgetc(input_file);
            if (ch != EOF){
                if (ch != ' ') {
                    putchar(ch);
                    }
                }
            }
    }
    My result is:
    Code:
    Thisis
    justa
    test
    Last edited by Giwrgows x; 12-25-2015 at 06:32 AM.

  2. #2
    Registered User
    Join Date
    Apr 2015
    Posts
    42
    What your read_input does is simply printing what's inside the text file except when there is ' ' it doesn't print it. So it's clear that a text file like

    Code:
    This is just a
    test
    will print

    Code:
    Thisisjusta
    test
    What you should do is changing space characters with new line characters so whenever code encounters space in the text it will print new line character. Also don't forget to check multiple space characters in a row or there will be blank lines between words.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    47
    are you talking about some kind of this:
    Code:
    if (ch == ' ')
    putchar('\n')
    how can i check for double characters since im getting 1 everytime with fgetc

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    42
    Quote Originally Posted by Giwrgows x View Post
    how can i check for double characters since im getting 1 everytime with fgetc
    You can have 2 chars c1 and c2. Before you read new char on c1 you can assign c1 to c2 then read new char to c1. However this is so pointless way of doing this. I recommend checking fscanf function which allows you to read formated data from file. So you can read string from file and print it with new line character at the end of it in a loop which ends when function counters EOF.

    fscanf - C++ Reference

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-07-2012, 02:50 AM
  2. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  3. C file print Issue in Line feed
    By muni in forum C Programming
    Replies: 3
    Last Post: 04-21-2008, 09:29 AM
  4. trying to print a line from a text file
    By kryonik in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2006, 09:14 PM
  5. print line by line from a file
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 02-18-2005, 01:36 PM