ftell() function giving wrong answer - c

when I run the program and gave 3 characters then the output is showing there are 9 characters. what is wrong in the code.
#include<stdio.h>
int main()
{
FILE *fp;
char c;
fp=fopen("random","w");
while((c=getchar())!=EOF)
{
putc(c,fp);
}
printf("no of character entered = %ld\n",ftell(fp));
fclose(fp);
}

Related

Why is the rest of my code is being ignored after I only have sent the first input?

I'm trying to write a program that asks for input of the name of a file and a char to be counted inside the file. But whenever I input the proper name of a file (like, "file.txt") it jumps right to the end of the program with a output like this:
"Name of the file: file.txt
Character to be counted:
The char occurs 0 times in the file "
...but I couldn't even type the char to be counted.
I know it's not an issue with the name of the file, because if I put the wrong name, it goes for the output I programmed for.
Would anyone care to explain me what's happening?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char filename[128];
printf("Name of the file: ");
scanf("%s", filename);
FILE * test = fopen(filename, "r");
if (test == NULL) {
printf("Error!!\n");
exit(1);
}
char inpt;
printf("Character to be counted:\n");
scanf("%c", &inpt); //gets the character to be counted
int count = 0;
char search = fgetc(test);
while(search != EOF) {
if (search == inpt) count++;
search = fgetc(test);
}
printf("The char occurs %d times in the file\n", count);
return 0;
}

Trying to sum values from a text file, receiving unrecognized character as output?

Using C, I am trying sum up the numbers in a file. The file contains numbers such as:
123
456
788
...
356
When running the code, it properly asks for input and prints the number I enter. However, it does not sum the file, and just displays an unrecognized character symbol, like a small ?. I don't think the number is over the alloted INT_MAX_SIZE. What seems to be the issue?
#include <stdio.h>
main() {
//Number variable to assign each line to
int c;
int fds[2];
int childid;
int size;
int number;
int sum;
printf ("Enter the number of processes to create: ");
scanf ("%d", &number);
printf ("You entered: %d", number);
printf("\n");
//File I/O operations
FILE *file;
//Open file for reading
file = fopen("Project1_OS/project1_data/file1.dat", "r");
//If file is found
if (file) {
//While file has data to be read
while ((c = getc(file)) != EOF)
//Print data
//putchar(c);
sum+=c;
//Close the file I/O
fclose(file);
}
putchar(sum);
}
first getc it's a function for reading characters from a file not integers .
you have to use fscanf :
fscanf(file,"%3d",&c)
second putchar it's a function for printing characters not intgers .
so you have to write :
printf("%d",sum);

Write a program to count the number of times a character appears in the File. (Case insensitive... 'a' and 'A' are considered to be the same)

I have to write a program to count the number of times a character appears in the File. (Case insensitive... 'a' and 'A' are considered to be the same)
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
FILE *fp1;
char ch,f[100],c,d;
int ct=0;
printf("Enter the file name\n");
scanf("%s",f);
fp1=fopen(f,"r");
printf("Enter character:");
scanf(" %c",&c);
do
{
printf("%c",ch);
ch=fgetc(fp1);
d=toupper(ch);
printf("%c",d);
if(c==d)
++ct;
}while(ch!=EOF);
fclose(fp1);
printf("\n");
printf("%d",ct);
return 0;
}`
This is the program I have written but the output i'm getting it is..
[ a.txt contains the string-
aaa ]
Now when running the program this is the output which I get :
Enter the file name
a.txt
Enter character:a
aAaAa
0
What am I doing wrong here ??
What you need is to check if the character to be searched is equal to a character in the file or its uppercase version and if it is, increment ct.
Simply change
if(c==d)
to
if(c==d || c==ch)
Other problem: ch is not initialized here
printf("%c",ch);
in the first iteration of the do...while loop. Fix it by moving the above printf after
ch=fgetc(fp1);
Also, add a check to see if ch is not EOF before printing it.
If you input 'a', and you transform all your characters toUpper()... It can definitely not work ;:=)
This code works i guess.
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp1;
char c;
char ai,s;
char fname[20];
int count=0;
clrscr();
printf("enter the character to be counted:");
scanf("%c",&ai);
s=toupper(ai);
printf("enter the file name :");
scanf("%s",&fname);
fp1=fopen(fname,"r");
if(fp1==NULL)
{
printf("cannot open this file");
}
do
{
c=fgetc(fp1);
if(c==ai || c==s)
{
count=count+1;
}
}
while(c != EOF);
printf("\nFILE '%s' has %d instances of letter %c",fname,count,ai);
getch();
}

counting words excepts the whitespace from file

I am trying to count words from file called file.txt but it gives me charecters with whitespace.
How to count words without counting the whitespace?
#include<stdio.h>
#include<conio.h>
main()
{
FILE *f1;
char c;
clrscr();
printf("data output");
f1 = fopen("file.txt","r");
while((c=getc(f1))!=EOF)
{
printf("%c",c);
}
fclose(f1);
getch();
}
Please help me to solve it as soon as possible.
Thanks in advance.
// Create a char array to store a word
char word[100];
// Stop when fscanf returns 0
while(fscanf(f1, "%s", word)==1)
{
// print the word
printf("%s ",word);
// Increment count
count ++;
}
// Print count
printf("%d\n", count );
Remember it assumes that no word is longer than 100 characters since fscanf does't check for buffer overflow

reversing text file in C

I wrote the following code but when i enter "abcd" it shows "dcb" and skips the first character.I know that my logic in the while loop crosses the file boundary but fseek(f2) is still not 0 when it crosses the file boundary.It should return some negative value.
#include<stdio.h>
int main()
{
FILE *f1,*f2;
char ch;
clrscr();
f1=fopen("Input","w");
while((ch=getchar())!=EOF)
putc(ch,f1);
fclose(f1);
f2=fopen("Input","r");
fseek(f2,-1L,2);
while(ftell(f2)!=0)
{
ch=getc(f2);
printf("%c",ch);
fseek(f2,-2L,1);
}
fclose(f2);
getch();
return(0);
}
You need a do-while loop, not a while-do loop.
You need to read the character when ftell() returns zero, but NOT read anymore. That's usually the indication you need a bottom-tested loop and not a top tested one.
#include <stdio.h>
int main(){
FILE *fp;
int ch;
fp=fopen("out.txt","w");
while((ch=getchar())!=EOF)
fputc(ch,fp);
fclose(fp);
fp=fopen("out.txt","rb");
fseek(fp,0L,SEEK_END);
while(fseek(fp,-2L,SEEK_CUR)==0){
ch=fgetc(fp);
putchar(ch);
}
fclose(fp);
return(0);
}

Resources