'How to add a sentence to end of a specifics line in text file using C?
i have a text file that includes words in each line like TEST, TEST1, TEST! and i have to use 3 proccess to check these words. Proccess one should check if the word consists of only letters. proccess two should check if the word contains numbers, proccess three should check if the word contains special characters.
Imagine that first line in text file is the word "TEST". Proccess one, should add "-Only Letters-" to end of line. These operations should be applied on the same txt file. I wrote this code:
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
int main()
{
char *filename = "kaynak.txt";
FILE *fp = fopen(filename, "rb+");
if (fp == NULL)
{
printf("Error: file not exist! %s", filename);
return 1;
}
char ch;
char line[10000];
bool write = false;
char sayi[10000] = "-Sayi var -";
while(fgets(line,sizeof(line),fp) != NULL){
int i = 0;
while(line[i] != '\0')
{
if(line[i] >= '0' && line[i] <= '9')
{
write = true;
printf("Number find\n");
}
i++;
}
if(write == true)
{
fputs("- Number - \n", fp);
write = false;
}
}
fclose(fp);
return 0;
}
This code replaces these lines:
2
Mart'ta
sunu8lan
in my sample text file with:
2
- Number -
u8lan
but it should be like this:
2 - Number -
Mart'ta
sunu8lan - Number -
How can i fix my algorithm?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
