Thread: Read and print from a file line by line and print >> at the end of each line

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    101

    Read and print from a file line by line and print >> at the end of each line

    Hi,

    I am making an easy program that reads a text from a file and prints the content line by line indicating each number of line and printing >> at the end of each line.

    This is the file: "prueba.txt" and its content:

    Escribir un programa que abra un fichero llamado prueba.txt
    previamente creado con el block de notas
    y guardado en la misma carpeta que el programa
    y que muestre el contenido del mismo por pantalla por lineas
    y cada linea con su numero de linea seguido de >>

    This is the code I have:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE_TEXTO 1000
    
    
    int main()
    {
        FILE *file;
        char texto[SIZE_TEXTO];
    
    
        file=fopen("prueba.txt", "r");
        int linea=0;
    
    
        while (fgets(texto,sizeof(texto),file)!=NULL) {
            printf("%d %s>>", ++linea, texto);
        }
        puts("");
        fclose(file);
        return 0;
    }
    The program do exactly what I want except prints >> in the next line:

    Output:

    1 Escribir un programa que abra un fichero llamado prueba.txt
    >>2 previamente creado con el block de notas
    >>3 y guardado en la misma carpeta que el programa
    >>4 y que muestre el contenido del mismo por pantalla por lineas
    >>5 y cada linea con su numero de linea seguido de >>>>


    Process returned 0 (0x0) execution time : -0.000 s
    Press any key to continue.

    I want this output if possible:

    1 Escribir un programa que abra un fichero llamado prueba.txt>>
    2 previamente creado con el block de notas>>
    3 y guardado en la misma carpeta que el programa>>
    4 y que muestre el contenido del mismo por pantalla por lineas>>
    5 y cada linea con su numero de linea seguido de >>>>


    Process returned 0 (0x0) execution time : -0.000 s
    Press any key to continue.

    How can I fix it?

    Thank you in advance.

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    fgets() read the whole line inclusive the newline. You must delete this newline.
    1) Include the header 'string.h'
    2) define a pointer to character
    3) use strchr() to find the newline and if one found, overwrite it with the string-terminator
    Code:
    #include <string.h>
    …
    char *x;
    …
        while(fgets(texto,sizeof(texto),file)!=NULL) {
            if ((x = strchr(texto, '\n'))) *x = '\0';
            printf("%d %s >>\n", ++linea, texto);
        }
    …
    Other have classes, we are class

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    101
    Worked perfectly, thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from a file and print in a different line
    By Giwrgows x in forum C Programming
    Replies: 3
    Last Post: 12-25-2015, 08:03 AM
  2. Replies: 3
    Last Post: 04-26-2013, 06:21 AM
  3. Replies: 3
    Last Post: 03-12-2013, 09:58 PM
  4. how to open files and print line by line in shell
    By omega666 in forum Linux Programming
    Replies: 4
    Last Post: 04-15-2011, 04:54 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

Tags for this Thread