Why is there a space after the first letter? - c

I have this code:
void text(){
FILE *fp;
fp = fopen( "Text.txt", "w" );
if (fp == NULL)printf("not open\n");
char txt[100];
char c;
while(1){
char c = getche();
if (c == '\e')
break;
txt[0] = c;
gets(txt+1);
fprintf(fp, "%s ",txt);
}
fclose(fp);
}
It works fine and the output would be like: Hello there.
void text(){
char fname[20] ;
puts("Write file name");
scanf("%s",&fname); //input of the file name
char ext[5] = ".txt";
char fileSpec[strlen(fname)+strlen(ext)+1]; //file name assambly
snprintf( fileSpec, sizeof( fileSpec ), "%s%s", fname, ext);
FILE *fp;
fp = fopen( fileSpec, "w" );
if (fp == NULL)printf("not open\n");
char txt[100];
char c;
while(1){
char c = getche();
if (c == '\e')
break;
txt[0] = c;
gets(txt+1);
fprintf(fp, "%s ",txt);
}
fclose(fp);
}
I took the top half from a website, tested it separately and it worked. The bottom half is the same and when i put the two together the output would be like this: H ello there.
Why is that space there?

You didn't provide enough context (i.e. What was put into stdin? What was in the file?).
But your fprintf(fp, "%s ",txt); has a space in it, and that seems like the likely culprit. (Note that the while loop can run more than one time, thus adding spaces between what it outputs to the file).

Related

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;
}

Search for a word in file line by line in C programming

I am trying to write code to search for a word in a file line by line and print the line containing the word.
Functions fgets and getline didn't seem to work.
void FindWord(char *word , char *file){
char *line ;
line = (char*)malloc(1024) ;
int fd ;
fd = open(file , O_RDONLY );
while (fgets(line , sizeof(line) ,fd )!= NULL)
{
if (strstr(line , word )!= NULL)
{
printf("%s",line);
}
}
}
Problem 1
fgets() needs a FILE* not a file descriptor.
Change
int fd ;
fd = open(file , O_RDONLY );
to
FILE* fp = fopen(file, "r");
and use fp as the argument to fgets.
Problem 2
sizeof(line) doesn't evaluate to 1024, as you are probably expecting. It just evaluates to the size of a pointer, which is most likely 4 or 8.
Change
while (fgets(line , sizeof(line) ,fd )!= NULL)
to
while (fgets(line , 1024, fp )!= NULL)
Update
Also, since you are hard coding 1024 in the call to malloc, you might as well use an array. Then, you can use sizeof(line).
void FindWord(char *word , char *file){
char line[1024] ;
FILE* fp = fopen(file, "r") ;
while (fgets(line , sizeof(line) , fp )!= NULL)
{
if (strstr(line , word )!= NULL)
{
printf("%s",line);
}
}
}
Couple of things are wrong.
You should not use open and work with file descriptors, if you don't have to.
Also, fgets needs FILE as 3. argument, not file descriptor. Use FILE macro:
FILE *f = fopen("myfile.txt", "r");
// Use f here
fclose(f);
Second, you allocated memory and never release it.
char *line = malloc(1024 /* *sizeof(char)*/);
//Some code here
free(line);
sizeof(line) returns size of pointer, not buffer size.
fgets(line , n , f) reads n bytes, not line. It reads at most n bytes, stops on '\n' or EOF. Use getline instead.
char *line_buffer = NULL;
size_t n = 0;
FILE *f = fopen(...);
getline(&line_buffer, &n, f);
//use here
fclose(f);
free(line_buffer);
All combined:
void FindWord(char *word , char *file){
char *line = NULL;
size_t n = 0;
FILE *f = fopen(file, "r") ;
while (getline(&line_buffer, &n, f) != -1)
{
if (strstr(line , word )!= NULL)
{
printf("%s",line);
}
}
fclose(f);
free(line);
}

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 :)

Write to .txt file?

How can I write a little piece of text into a .txt file?
I've been Googling for over 3-4 hours, but can't find out how to do it.
fwrite(); has so many arguments, and I don't know how to use it.
What's the easiest function to use when you only want to write a name and a few numbers to a .txt file?
char name;
int number;
FILE *f;
f = fopen("contacts.pcl", "a");
printf("\nNew contact name: ");
scanf("%s", &name);
printf("New contact number: ");
scanf("%i", &number);
fprintf(f, "%c\n[ %d ]\n\n", name, number);
fclose(f);
FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
/* print some text */
const char *text = "Write this to the file";
fprintf(f, "Some text: %s\n", text);
/* print integers and floats */
int i = 1;
float pi= 3.1415927;
fprintf(f, "Integer: %d, float: %f\n", i, pi);
/* printing single characters */
char c = 'A';
fprintf(f, "A character: %c\n", c);
fclose(f);
FILE *fp;
char* str = "string";
int x = 10;
fp=fopen("test.txt", "w");
if(fp == NULL)
exit(-1);
fprintf(fp, "This is a string which is written to a file\n");
fprintf(fp, "The string has %d words and keyword %s\n", x, str);
fclose(fp);
Well, you need to first get a good book on C and understand the language.
FILE *fp;
fp = fopen("c:\\test.txt", "wb");
if(fp == null)
return;
char x[10]="ABCDEFGHIJ";
fwrite(x, sizeof(x[0]), sizeof(x)/sizeof(x[0]), fp);
fclose(fp);

Resources