Skip to content

Instantly share code, notes, and snippets.

@marceloboeira
Last active August 29, 2015 14:01
Show Gist options
  • Save marceloboeira/fd7cd193a6d4ff5d67b2 to your computer and use it in GitHub Desktop.
Save marceloboeira/fd7cd193a6d4ff5d67b2 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<string.h>
#define FILE_NAME "data.txt"
#define MAX_CHARS 100
FILE *getFile(int write) {
return (write == 1) ? fopen(FILE_NAME,"a+") : fopen(FILE_NAME,"r+");
}
void writeFile(char *b) {
FILE *f = getFile(1);
fputs(b,f);
fclose(f);
}
int main(){
char *buffer;
buffer = malloc(MAX_CHARS * sizeof (char) );
printf("Write the file contents:");
scanf("\n%s",buffer);
writeFile(buffer);
printf("\nThanks!\n");
}
#include <stdio.h>
#include <string.h>
#define FILE_NAME "data.txt"
#define MAX_CHARS 10000
FILE *getFile(int write) {
return (write == 1) ? fopen(FILE_NAME,"w") : fopen(FILE_NAME,"r");
}
char *loadFile () {
FILE *f = getFile(0);
char *final,_lineBuffer[500];
int j = 0;
final = malloc(MAX_CHARS * sizeof(char));
while (fgets(_lineBuffer, 500, f)!= NULL){
strcat (final, _lineBuffer);
}
fclose(f);
return final;
}
int main(){
char *bf,*sc, *fc = loadFile();
printf("The File Contents: \n%s\n",fc);
bf = malloc(100 * sizeof (char) );
printf("\nType something:");
scanf("%s",bf);
sc = strstr(fc, bf);
if (sc) {
printf("\n\"%s\" was found in the file!\n",bf);
}
else {
printf("\n\"%s\" was NOT found in the file!\n",bf);
}
}
marCelo
teste
palavra
engineer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment