Created
June 11, 2011 12:30
-
-
Save lefty313/1020519 to your computer and use it in GitHub Desktop.
file_operation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
void print_char_vector(char * array) | |
{ | |
int i; | |
printf("\n"); | |
for (i = 0; i < strlen(array); ++i) printf("\%c",array[i]); | |
} | |
int * create_int_vector(int i) | |
{ | |
int *vector; | |
vector = (int *) calloc(i,sizeof(int *)); | |
return vector; | |
} | |
int count_signs(FILE * my_file, char sign_to_find) | |
{ | |
char sign; | |
int i=0; | |
// idz do poczatku lini | |
fseek(my_file, 0, 0); | |
while((sign=fgetc(my_file))!=EOF) | |
{ | |
if (sign == 'l') i++; | |
} | |
return i; | |
} | |
int count_words(FILE * my_file) | |
{ | |
int count=0; | |
char sign; | |
// idz do poczatku lini | |
fseek(my_file, 0, 0); | |
while((sign=fgetc(my_file))!=EOF) | |
{ | |
if(sign==' ' || sign=='\t' || sign=='\n' || sign=='\r') | |
{ | |
{ | |
count++; | |
} | |
} | |
} | |
count++; | |
return count; | |
} | |
int * find_string(FILE * my_file, char word_to_find[]) | |
{ | |
int i, count=1; | |
int b=1, wynik=0; | |
char sign; | |
int *summary = create_int_vector(2); | |
summary[0] = 0; // flaga | |
summary[1] = 1; // kolumna | |
summary[2] = 1; // wiersz | |
// idz do poczatku lini | |
fseek(my_file, 0, 0); | |
while((sign=fgetc(my_file))!=EOF && b==1) | |
{ | |
if(sign=='\n') {(summary[1])++; summary[2]=0;} | |
if(sign==word_to_find[0]) | |
{ | |
for(i=1;i<strlen(word_to_find);i++) | |
{ | |
if((sign=fgetc(my_file))!=EOF && sign==word_to_find[i]) | |
{ | |
count++; | |
} | |
else | |
{ | |
i=strlen(word_to_find); | |
count=1; | |
if(sign=='\n') (summary[1])++; | |
} | |
} | |
if (count==strlen(word_to_find)) | |
{ | |
b=2; | |
(summary[2])--; | |
summary[0]=1; | |
} | |
} | |
(summary[2])++; | |
} | |
return summary; | |
} | |
void merge_file(FILE * file1, FILE * file2, FILE * output_file) | |
{ | |
char sign; | |
fseek(file1,0,0); | |
fseek(file2,0,0); | |
fseek(output_file,0,0); | |
while((sign=fgetc(file1))!=EOF) fputc(sign,output_file); | |
while((sign=fgetc(file2))!=EOF) fputc(sign,output_file); | |
} | |
int main() | |
{ | |
int count = 0; | |
int ch,i; | |
FILE *my_file; | |
FILE *file1 = fopen("plik2.txt","r"); | |
FILE *file2 = fopen("plik3.txt","r"); | |
FILE *output = fopen("output.txt","a"); | |
int *result = create_int_vector(2); | |
char string_to_find[] = {"jesc"}; | |
char sign = 'l'; | |
my_file = fopen("plik1.txt","r"); | |
printf("\nw pliku znajduje sie %d slow",count_words(my_file)); | |
printf("\nznak %c wystepuje w pliku %d razy",sign,count_signs(my_file,sign)); | |
result = find_string(my_file,string_to_find); | |
if (result[0] == 0) | |
{ | |
printf("\nW pliku nie ma szukanego slowa"); | |
} | |
else | |
{ | |
printf("\nSzukane slowo znajduje sie w %d wierszu i %d kolumnie",result[1],result[2]); | |
} | |
merge_file(file1,file2,output); | |
fclose(my_file); | |
fclose(file1); | |
fclose(file2); | |
fclose(output); | |
getchar(); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment