READ File line by line using C - c

I am working on a program that reads a txt file line by line and copies each line into an empty text file. First the program reads characters until the end of line, and stores all characters read in a buffer. After that it writes the buffer to a text file, and repeats this process until it reaches the end of file. The problem that I'm facing is: the program writes only the first line in the empty file. Can you help to solve this problems please, I'm a newbie in c.
this content txt file i want to read:
hey everyone
how are you
goood
and this the output of the programme:
hey everyone
this the Source code:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int GetFileLenght(FILE*FP);
int main()
{
FILE *SourceFile;
FILE *DestinationFile;
int c;
int i=-1;
int x=1;
char buffer[350];
int lenght;
lenght=GetFileLenght(SourceFile);
printf("%d",lenght);
SourceFile=fopen("D:/SourceFile.txt","r");
while(i!=lenght)
{
c=fgetc(SourceFile);
printf("%c",c);
switch(c)
{
case '\n' :
DestinationFile=fopen("d:/des.txt","a+");
fputs(buffer,DestinationFile);
*buffer=0;
fclose(DestinationFile);
break;
default:
i++;
buffer[i]=c;
printf("%s",buffer);
}
}
return 0;
}
int GetFileLenght(FILE*FP)
{
FP = fopen("d:/SourceFile.txt", "a+");
fseek(FP, 0, SEEK_END);
int lengthOfFile = ftell(FP);
fclose(FP);
return lengthOfFile;
}
EDIT:
I have Made some changes to the program ,now it's good:
I'am also wondering if there is another way to add new line to file using fputs ?
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
FILE *SourceFile;
FILE *DestinationFile;
int c=0;
int i=-1;
int x=1;
char buffer[350];
SourceFile=fopen("D:/SourceFile.txt","r");
DestinationFile=fopen("d:/des.txt","a+");
while(x)
{
c=fgetc(SourceFile);
switch(c)
{
case '\n' :
fputs(buffer,DestinationFile);
fprintf(DestinationFile,"\n");
printf("%s",buffer);
strncpy(buffer, "", sizeof(buffer));
i=-1 ;
break;
case EOF :
fputs(buffer,DestinationFile);
x=0;
default:
i++;
buffer[i]=c;
}
}
fclose(DestinationFile);
return 0;
}

Related

Finding number of characters,words and lines in a text file

Here is the .c file followed by the functions in my .h file
#include <stdio.h>
#include "functions.h"
#define INPUT_FILE "C:/Users/user/Desktop/test.txt"
int main(){
FILE *text_file;
int num_characters, num_words, num_lines;
text_file = fopen(INPUT_FILE,"r");
if(text_file == NULL){
printf("!!-------ERROR OPENING FILE------!!\nclosing program....");
return 0;
}
num_characters = read_characters(text_file);
num_words = read_words(text_file);
num_lines = read_lines(text_file);
printf("Number of Characters: %d\nNumber of Words: %d\nNumber of Lines: %d\n",num_characters,num_words,num_lines);
return 0;
}
#include <stdio.h>
#include "functions.h"
#define INPUT_FILE "C:/Users/Lott-kerby/Desktop/test.txt"
#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_
#include <stdio.h>
int read_characters(FILE *text_file){
int i;
int char_count = 0;
while((i = fgetc(text_file)) !=EOF)
char_count++;
return char_count;
}
int read_words(FILE *text_file){
char j;
int word_count = 0;
while((j = fgetc(text_file)) != EOF){
if(j == ' ')
word_count++;
}
return word_count;
}
int read_lines(FILE *text_file){
char k;
int line_count = 0;
while((k = fgetc(text_file)) != EOF){
if(k == '\n')
line_count++;
}
return line_count;
}
The goal is to find the number of characters words and lines in the text file. I get the correct number of characters when i run but I get the incorrect number of words and lines. The text file I am using is as follows:
word
word
word
with this .txt my program out put is:
Number of characers:14
NUmber of words: 0
Number of Lines: 0
any help would be greatly appreciated. The "words" are on their own line each in my text file.
Well you count the nunmber of words by counting the number of spaces because you assume there is a space between every word. but in your example input file there are no spaces.
So you may want to add a check for space OR new line.
Also you may want to return word_count+1 and line_count+1 because a single line without a newline should return 1. And the same is true for a single word with no space
EDIT: oouuhh and now I see that you read the file multiple times without resetting the file pointer so fgetc will always instantly return EOF in read_words() and read_lines() ... reset it using
rewind ( text_file );

How to read and compare character 255 in C

I need to write a code that searches all the aparition of a string in a file. Recently, my teacher told me to search string (char)255(char)255 in a file with the same string. The problem is that I can not read those characters and badly, I cannot distinguish or compare those caracter to EOF; My code for searching the given string in a file is :
//problema 14
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char b;
int k=0;
if(argc!=3) {
fprintf(stderr,"Utilizare: %s fisier sir\n",argv[0]);
return 1;
}
if(argv[2][0]=='\0'){
fprintf(stderr, "String vid\n");
return 1;
}
FILE *f;
f=fopen(argv[1],"r");
if (!f)
{
perror(argv[1]);
return 1;
}
int i=0;
while((b = fgetc(f))!=EOF)
{
if(b==argv[2][i]) i++;
else {
fseek(f,-i, SEEK_CUR);
i=0;
}
if(argv[2][i]=='\0'){
k++;
fseek(f,-i+1, SEEK_CUR);
i=0;
}
}
printf("\nSULFUS %d APPEARANCES\n",k);
return EXIT_SUCCESS;
}
what can I do with this code to work on comparint string of (char)255 characters?
The trick is to realize that fgetc returns an int.
So, change the data type of bto int!
But then, "while" does nothing, because it seems that 255 is also recognized as EOF or it goes infinite. I tried also with int but I couldn't figure out much...This teacher is like a compilator, he likes to put you in trouble.

Cannot print characters from a file

I am trying to read a file character by character and print it on screen.
However, the character is not displaying, I am getting a box with 0001 in it.
This is my code
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
int ch;
fp=fopen("myfile.txt", "rb");
while((ch = getc(fp)) !=EOF){
putc(ch, stdout);
}
fclose(fp);
return 1;
}
you need to check the return values from fopen, to ensure you opened the file successfully, you could be executing from the wrong directory.
Plus if your file is a TEXT file, you should be opening using "rt".
Basic File Opening modes in C is
"r"-reading
"w"-writing
"a"-append
"r+"-reading+writing
"w+"-reading+writing
"a+"-"reading+appending"
This code is enough to read .txt files
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
int ch;
fp=fopen("myfile.txt", "r");
while((ch = getc(fp)) !=EOF){
putc(ch, stdout);
}
fclose(fp);
return 0;
}

Error accured in reading files by using line by line in c

log.txt :
Hello world
world is not enough
to show our knowledge
cpp file :
#include <stdio.h>
#include <string.h>
int main()
{
char szLine[512+1]={0};
FILE *fp=fopen("log.txt", "r");
while(!feof(fp))
{
fscanf(fp, "%512[^\n]", szLine);
puts(szLine);
getchar();
}
return 0;
}
Acually expected output for this program has to read line by line. But it read only first line alone. What is the mistake on this code. thanks advance.
you can change your program like this:
#include <stdio.h>
#include <string.h>
int main()
{
char szLine[512+1]={0};
FILE *fp=fopen("log.txt", "r");
while(!feof(fp))
{
fgets(szLine,512,fp);
puts(szLine);
getchar();
}
return 0;
}
because the offset of the file stream always be 0, so you read
"%512[^\n]" does not read the endline character. The remaining part of the file starts with \n and fscanf fails to read the line(it return 0 instead of 1). You need to read the endline character !
#include <stdio.h>
#include <string.h>
int main()
{
int bla;
char szLine[512+1]={0};
FILE *fp=fopen("log.txt", "r");
while(!feof(fp))
{
bla= fscanf(fp, "%512[^\n]", szLine);
if(bla==1){
printf("%d\n",bla);
puts(szLine);
getchar();
fgetc(fp);
}
}
return 0;
}
Using getline() may be a more reliable solution.
getline(&szLine,&size,fp);
Bye,
Francis
try this
while(fscanf(fp, " %512[^\n]", szLine)==1)
{
puts(szLine);
getchar();
}

copying text to a character array from a text file in C?

Hey there, How do I go about copying text inside a text file into a multidimensional character array?
supposing the text file( text_file.txt) contained
this is the first line
this is the second line
this is the third line
#include <stdio.h>
int main(void){
FILE *f;
f=fopen("text_file.txt","r");
if (f==NULL){
printf("invalid!");
return 1;
}
else {
printf("successful");
}
char copied_text[80][80];
while (!feof(f)){
int i=0,j=0;
fgets(copied_text[i][j],"%s",f);
i++;
}
return 0;
}
-thank you.
I think your code almost work.
Just move the declaration of int i out of the loop.
Change the first parameter of fgets to copied_text[i] because it needs a pointer here.
Change the second parameter of fgets to 80 because it should be a int indicates the acceptable string length.
#include <stdio.h>
int main(void){
FILE *f;
f=fopen("text_file.txt","r");
if (f==NULL){
printf("invalid!\n");
return 1;
}
else {
printf("successful\n");
}
char copied_text[80][80];
int i=0;
while (!feof(f)){
fgets(copied_text[i],80,f);
++i;
}
for(int i = 0; i <3; ++i)
printf("%s\n", copied_text[i]);
return 0;
}

Resources