0

I have the following data.txt file that I am reading:

b    1
b    2
b    3
b    4

It is a tab in-between

I have the following code to print it out, line by line, then character by character.

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

int printCharArray(char *arr);

int main(int argc, char * argv[]){
    FILE * filePointer;
    filePointer = fopen("data.txt", "r");
    char singleLine[32]; 

    while(!feof(filePointer)){
        fgets(singleLine, 32, filePointer);
        printCharArray(singleLine);
    }

    fclose(filePointer);
    return 0;
}

int printCharArray(char *arr){
    int length = 5;
    printf("\n");

    int i = 0;
    for(i=0;i<length;++i){
        printf("%c, ", arr[i]);
    }
    printf("DONE PRINTING \n");

    return 0;
}

What confuses me is what it prints out:

b,  ,  ,  , 1, DONE PRINTING 

b,  ,  ,  , 2, DONE PRINTING 

b,  ,  ,  , 3, DONE PRINTING 

b,  ,  ,  , 4, DONE PRINTING 

I don't understand what there are 3 spaces between the letter and the number. If they're all chars, shouldn't there just be one space for the tab, and that's it?

James Mitchell
  • 2,387
  • 4
  • 29
  • 59
  • 2
    Make sure that you really have tabs in data.txt and not spaces... – Sister Fister Sep 25 '17 at 03:05
  • https://stackoverflow.com/questions/29476556/read-files-separated-by-tab-in-c this can be helpful.. – Abhinesh Sep 25 '17 at 03:06
  • @SisterFister I pressed tab when I made the file – James Mitchell Sep 25 '17 at 03:07
  • 1
    [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Jonathan Leffler Sep 25 '17 at 03:08
  • 1
    @JamesMitchell Your editor probably converted your tabs to spaces. – Sister Fister Sep 25 '17 at 03:08
  • 2
    Your output says that the input file does not contain what you think it contains. In fact, you should use a hex dump program or equivalent to show what's in the file. You're making some dubious assumptions about line lengths in the function. (You could modify the printing to: `printf("(%d) %c, ", arr[i], arr[i]);` to see what the numeric character values are.) – Jonathan Leffler Sep 25 '17 at 03:09

1 Answers1

1

Here's a cleaned up minimal adaptation of your code, with better printing as suggested in my comment.

Code

#include <stdio.h>

void printCharArray(char *arr);

int main(void)
{
    char filename[] = "data.txt";
    FILE *filePointer = fopen(filename, "r");
    if (filePointer == NULL)
    {
        fprintf(stderr, "Failed to open file '%s' for reading\b", filename);
        return(1);
    }

    char singleLine[32];

    while (fgets(singleLine, sizeof(singleLine), filePointer) != 0)
        printCharArray(singleLine);

    fclose(filePointer);
    return 0;
}

void printCharArray(char *arr)
{
    printf("\n");
    for (int i = 0; arr[i] != '\0'; ++i)
        printf("(%d) %c, ", arr[i], arr[i]);
    printf("DONE PRINTING\n");
}

Here are the outputs from two versions of the data file, one with tabs as claimed and one with spaces.

Spaces

Note that the data was copied from the question. There are 4 spaces between the letter and the digit. The code also prints the newline; it would be easy to modify it to stop on newline or null byte.

(98) b, (32)  , (32)  , (32)  , (32)  , (49) 1, (10) 
, DONE PRINTING

(98) b, (32)  , (32)  , (32)  , (32)  , (50) 2, (10) 
, DONE PRINTING

(98) b, (32)  , (32)  , (32)  , (32)  , (51) 3, (10) 
, DONE PRINTING

(98) b, (32)  , (32)  , (32)  , (32)  , (52) 4, (10) 
, DONE PRINTING

Tabs

(98) b, (9)     , (49) 1, (10) 
, DONE PRINTING

(98) b, (9)     , (50) 2, (10) 
, DONE PRINTING

(98) b, (9)     , (51) 3, (10) 
, DONE PRINTING

(98) b, (9)     , (52) 4, (10) 
, DONE PRINTING
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278