Reputation:
I'm learning c by myself and I'm actually trying to code a program which count the numbers of TAb in a file and if a line has a tab, I want to print the whole line and the numbers of tabs in this line. And if it is not muhch more difficult, I want you to help me to do that also if a line has more than 80 characters, print this line and the numbers of characters I have this main function:
include <stdio.h> /* printf */
/* Prototype from tablen.c */
void tablen(const char *filename);
int main(int argc, char **argv)
{
if (argc < 2)
{
printf("Usage: tablen filename\n");
printf("where: filename - file to process.\n");
return -1;
}
tablen(argv[1]);
return 0;
}
This main function is very basic so I hope there is no error there. And this function too:
include <stdio.h> /* FILE, fopen, feof, fgets, fclose */
include <string.h> /* strlen */
void tablen(const char *filename)
{
/*Variables*/
int i; /*loop controller */
int tabs = 0; /*number of tabs*/
int line = 0; /*current line*/
int size_string; /*size of the string*/
File *file; /* open and read the file */
file = fopen(filename, "rt"); /*open the file for read text*/
size_string = strlen(filename);
/*if we can read the file*/
if(file)
{
/*while we don't reach the end of file, we still reading*/
while (!feof(file))
{
for(i = 0; i < size_string; i++)
{
if(filename[i] == 9) /*ASCII value of TAB is 9 or '\'*/
{
tabs++;
}
if(tabs > 0)
{
printf("# %i: (tabs: %i) |", line, tabs);
}
if(filename[i] == '\n')
{
line++;
tabs = 0;
}
}
}
}
}
I've write this pseudo-code, I think it is correct For counting tabs: First open a file for read/text while there are more line in the file(and reading one by one) we count the number of tabs if we found a line with tabs, print the line and the count of tabs Of course we close the file
For checking line lengths
First open a file for read/text and hile there are more lines in the file, we heck the length of each line. If the line is longer than 80 characters we print that line with length information
I don't know if I am in the correct way because is the first time I try to deal with files
Upvotes: 0
Views: 2189
Reputation: 316
For counting number of tabs in a line at a time, it is better to use getline() function. getline() reads a line from file stream and returns number of characters in read line. Read manual page of getline() for more information.
You can take a look at below code for the solution to your problem
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
int tabs=0,totaltabs=0,i;
fp = fopen(argv[1], "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
printf("Retrieved line of length %d\n", read);
for(i=0;i<read;i++){
if(line[i] == '\t')
tabs++;
}
if(tabs){
printf("line = %s\nNumber of tabs = %d\n",line,tabs);
totaltabs = totaltabs+tabs;
tabs=0;
}
if(read >=80)
printf("%s\n",line);
}
if (line)
free(line);
exit(EXIT_SUCCESS);
}
Upvotes: 1