Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How to print instead of \t the number of spaces that i determine *using files * in c?

using C,Converting each \t to a number of spaces ... Ex ,if my tabs.txt contains

This file has. too many. tabs. where each space above is actually a \t character, then the file myspaces.txt should look like the following if n=2, This. file has too many tabs. can my if statment be like if (c=='\t') then i d print a space ? i do have the code but its not runnging dunno why :-/

#include<stdio.h>

#include<stdlib.h>

int main()

{

char d;

FILE * fin = fopen("mytabs.txt", "r");

FILE * fout = fopen("myspaces.txt", "w");

int n;

printf("Enter n: ");

scanf("%d", &n);

while(fscanf(fin, "%c" ,&d)!=EOF)

{

if(d =='\t')

{

for(i=0;i<n;i++)

{

fprintf(fout , " ");

}

}

else {

fprintf(fout, "%c", d)

}

}

fclose(fin);

fclose(fout);

system("pause");

return 0;

}

1 Answer

Relevance
  • 9 years ago
    Favorite Answer

    You will need to keep track of what column number you are on. When you hit a tab, you output 1 or more spaces until you hit the next tab stop column.

    It will be easier if you start counting columns at 0 instead of 1.

    Think about a tab stop of 4. These are the columns and the tab stops:

    0 1 2 3 T 5 6 7 T 9 10 11 T 13 ...

    So, if you are in column 0, 1 or 2, you write spaces until you get to column 4. Column 5, 6 and 7 go to 8, etc.

    By starting at 0, it works out that the tab stop columns are all evenly divisible by your tab stop value. So you can test to see if you are on a tab stop just by doing column % n == 0.

Still have questions? Get your answers by asking now.