Counting position of element in a text file - c

I have a created name.txt file which contains name of students.
I want to find the position of each name using it in C.
here is my main C code for extracting name.txt .
FILE *fp;
char c;
char name[100];
fp = fopen("name.txt", "r");
int ct=0;
while(c != EOF)
{
c = fgetc(fp);
int i = 0;
name[i] = c;
printf("%c",name[i]);
}
And here is the contents name.txt file
1)Liam
2)Olivia
3)Noah
4)Emma
5)Oliver
6)Charlotte
7)Elijah
8)Amelia
9)Bob
10)Roy

Related

C Program to search a file and print certain words into a new file

So I need to write a C program that :
asks user to input letter of first name
open a file with a list of 1200 names
search the file for all names that begin with the letter inputted by user
write all found names into a new file named based on the letter inputted (ex. aNames.txt)
Here's what I have so far. I'm okay taking the original file and putting all the names in an array but I'm stuck for how to take a user input and get it to search for that first letter of every name. Should I use a 2d array instead? Sorry I'm very new please help! Thanks
#define SIZE 1200
int main()
{
FILE *filePtr;
int i = 0;
char arrChar[SIZE];
char name[20];
char letter;
printf("Enter letter to search names that start with it:");
scanf("%c", letter);
filePtr = fopen("names.txt", "r");
while(fscanf(filePtr, "%s", arrChar) != EOF)
#include <stdio.h>
char getNxtPntdVal(FILE *fPntr) {
char holder;
holder = fgetc(fPntr);
return holder;
}
void writeToFile(char c){
FILE *fpt = fopen("/home/jrinder/CLionProjects/untitled17/namesOut.txt","a");
fputc(c,fpt);
fclose(fpt);
}
int main() {
FILE *filePtr;
char arrChar[1200];
char name[20];
char letter;
char ch;
char ch2;
int charNum=0;
filePtr = fopen("/home/jrinder/CLionProjects/untitled17/names.txt","r");
printf("Letter:");
letter=getchar();
while((ch=fgetc(filePtr))!=EOF){
if(ch==letter && charNum ==0) {
ch2 = ch;
while (ch2 != '\n') {
printf("%c", ch2);
writeToFile(ch2);
ch2 = getNxtPntdVal(filePtr);
}
charNum=charNum+1;
printf("\n");
writeToFile('\n');
} else if(ch=='\n') {
charNum=0;
}else{
charNum=charNum+1;
}
}
}
Alternative solution, using strings IO (fscanf, fprintf), and not character by character IO (getchar/putchar):
Also arranging declaration - defer to the point when it's needed + combine with initial value, if possible ;
#define SIZE 1200
int main()
{
char letter;
printf("Enter letter to search names that start with it:");
scanf("%c", letter);
FILE *filePtr = fopen("names.txt", "r");
if ( !filePtr ) ... // error handling
char out_name[20] = "?Names.txt" ;
name[0] = letter ;
FILE *out = fopen(out_name, "w") ;
if ( !out ) ... // error handling
char arrChar[SIZE];
while(fscanf(filePtr, "%s", arrChar) != EOF)
if ( arrChar[0] == letter ) fprintf(out, "%s\n", arrChar) ;
}
fclose(out) ;
fclose(filePtr) ;

I need to take commands from text file word by word

I need to take words from a text file and do certain tasks for specific words. But ı can't take word as variables
#include <stdio.h>
#include <stdlib.h>
int main( )
{
char * filename = "file.txt";
FILE * fp = fopen(filename, "r");
if (fp == NULL) return 1;
char c;
int count = 0;
while((c = fgetc(fp)) != EOF)
{
if(c == ' ')
{
printf("\n");
}
else
{
printf("%c", c);
}
}
fclose(fp);
return 0;
}
this is what ı used for seperating words but ı can't use words to do something or call a function for example
Add name "xxxx" number "xxxxxxxx"
when word Add comes it takes name as "xxxx" then number
so it creates a virtual file in program with name and add number in it any help ?

Saving file as UTF-8

I am trying to write a program for translating English to Greek. So I find the ASCII number of the English character (a) and then saving the new char to a file. However is still saves 'á' and not 'α' cause they have the same decimal number.
int main(int argc, char *argv[]) {
FILE *fp1, *fp2;
char ch,demo;
int i;
fp1 = fopen( argv[1], "r");
fp2 = fopen("Translated.txt", "w");
while (1) {
ch = fgetc(fp1);
if (ch == EOF)
break;
else{
i = ch + 128;
demo = i;
putc(demo, fp2);
}
}
printf("File copied Successfully!");
fclose(fp1);
fclose(fp2);
return 0;
}
How can I save a file as UTF-8 in order to view it as a Greek character ?
Any other way of converting ISO8859-1 to ISO8859-7 ?

print the first letter of a file in C

Hello guys I am having a problem in printing the first two letter/characters of a .txt file which contains --> "need help". I would like to print the first two letters --> "ne". I tried with ch[], but I couldnt fix, so i changed it back to the part which works:
int main() {
char ch, file_name[2];
int i;
FILE *fp;
printf("Enter the name of file you wish to see\n");
gets(file_name);
fp = fopen(file_name,"r");
if( fp == NULL )
{
printf("Error while opening the file.\n");
exit(1);
}
printf("The contents of %s file are :\n", file_name);
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
fclose(fp);
return 0;
}
int main() {
char ch[2];
FILE *fp;
fp = fopen("file.txt","r");
fread(ch,2,1,fp);
printf("(%c%c) (%2.2s)",ch[0], ch[1],ch);
}
stdout :
(ne) (ne)
I don't know why you need only the two first letters, but here's how to do it.
char file_name[256];
gets(file_name);
int lenght = 0;
strlen(file_name) > 2 ? lenght = 2: lenght = strlen(file_name);
for(int i = 0; i < lenght; i++)
printf("%c", file_name[i]);
But an advice that I can give you for strings in C (char arrays) is try to always create a bigger array that you need. It doesn't cost much memory and it's always safer to have more than enough. When you call standards functions like printf(), they will check the null terminated character and this will defines the size of your string.
This is what i came up so far. It prints the first two characters, but then it prints questions marks within a square underneath.
Here is the code:
int main() {
char ch[2], file_name[100];
int i;
FILE *fp;
printf("Enter the name of file you wish to see\n");
gets(file_name);
fp = fopen(file_name,"r");
if( fp == NULL )
{
printf("Error while opening the file.\n");
exit(1);
}
printf("The contents of %s file are :\n", file_name);
fscanf(fp, "%2s", ch);
printf("%s\n", ch);
while( ( ch[i] = fgetc(fp) ) != EOF ){
printf("%c",ch);
}
fclose(fp);
return 0;
}

C - how to delete string the same string as user input from a file in c?

I want to make a program that delete a String that the same as user input from a file
below are contents in the file
G12
G13
G14
For example user input G13 , expected output is as following :
G12
G14
I got some idea that make a temporary file , but don't get any idea on how to get a string line by line because i print the file content using these code
if(file!=NULL)
{
while ((c = getc(file)) != EOF)
putchar(c);
fclose(file);
}
so basically i reads all the file content only letter by letter (dont have any idea how to make it reads word by word
Thanks in advance
simple line by line sample
#include <stdio.h>
#include <string.h>
char *esc_cnv(char *str){
char *in, *out;
out = in = str;
while(*in){
if(*in == '\\' && in[1] == 'n'){
*out++ = '\n';
in += 2;
} else
*out++ = *in++;
}
*out = '\0';
return str;
}
int main(void){
FILE *fin = stdin, *fout = stdout;
char line[1024];
char del_str[1024];
char *p, *s;
int len;
int find=0;//this flag indicating whether or not there is a string that you specify
fin = fopen("input.txt", "r");
fout = fopen("output.txt", "w");//temp file ->(delete input file) -> rename temp file.
printf("input delete string :");
scanf("%1023[^\n]", del_str);
esc_cnv(del_str);//"\\n" -> "\n"
len = strlen(del_str);
while(fgets(line, sizeof(line), fin)){
s = line;
while(p = strstr(s, del_str)){
find = 1;//find it!
*p = '\0';
fprintf(fout, "%s", s);
s += len;
}
fprintf(fout, "%s", s);
}
fclose(fout);
fclose(fin);
if(find==0)
fprintf(stderr, "%s is not in the file\n", del_str);
return 0;
}
Exactly as Micheal said. On a file you can do only write and read operation. So you must read the char and when you find the exact word that you don't want to write, just don't write it. Otherwise you write it :)

Resources