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);
}
Related
Here I am writing some data to a file and trying to display that data.Is it compulsory to use fscanf before printing that data on the output screen? When I use the fscanf and suppose I enter the details as:
Ram
usa
12
the output comes as
Ramusa1212
What might be the possible error here?
#include <stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(){
FILE *fptr;
char name[20],address[20];
int roll;
fptr=fopen("1.txt","w");
if(fptr!=NULL){
printf("file created successfully:you can write into the file now");
}
else{
printf("error creating file");
exit -1;
}
printf("enter your name,address,roll number");
fgets(name,20,stdin);
scanf("%s",address);
scanf("%d",&roll);
fprintf(fptr,"%s%s%d",name,address,roll);
fclose(fptr);
fptr=fopen("1.txt","r");
fscanf(fptr,"%s%s%d",name,address,&roll);
printf("%s%s%d",name,address,roll);
fclose(fptr);
getch();
}
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);
}
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();
}
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
The following code reads from a file the occurrences of the digit 1. My question is how could such an occurrence be replaced with another number (say '4') and written back again in the file. The while loop will be continued with?
int next;
FILE *f;
if (!(f=fopen("C:\\Test\\Sign.txt", "rt")))
{
printf("File not existing\n");
}
else{
while((next=='1')!=EOF)
The proper solution for that [including the initial one] would be:
#include <stdio.h>
#include <conio.h>
int f(FILE *);
int main(){
int num=0, next;
FILE *f;
if (!(f=fopen("C:\\Test\\Sign.txt", "rt")))
{
printf("File not existing\n");
}
else{
for(;;){
if((next=fgetc(f))== EOF) break;
if (next == '1') num++;}
}
printf("Found occurrences of digit 1 are %d\n", num);
getch();
}
The for loop counts the occurrences of ones[in this case] whenever it passes through the number with fgetc assigned to the value of next which is 1.