This commit is contained in:
2026-02-23 09:46:54 +01:00
parent 28dee070b3
commit 28546c7ce1
5 changed files with 22 additions and 65 deletions
@@ -25,8 +25,8 @@ int read_n_bytes(const char *filename, size_t n){
} }
fread(cara, sizeof(char), n, file); fread(cara, sizeof(char), n, file);
//cara[n+1] = '\0'; cara[n+1] = '\0';
//printf("%s\n", cara); printf("%s\n", cara);
free(cara); free(cara);
fclose(file); fclose(file);
return 0; return 0;
@@ -11,7 +11,7 @@ int write_one_by_one(char *filename, const char *text){
index ++; index ++;
} }
//fputc(*text, file); //fputc(*text, file);
fputc('\0', file); //fputc('\0', file);
fclose(file); fclose(file);
return 0; return 0;
} }
@@ -21,7 +21,9 @@ int write_all_in_one(char *filename, const char *text){
FILE *file = fopen(filename, "w"); FILE *file = fopen(filename, "w");
if(!file)return 1; if(!file)return 1;
fprintf(file, text); fprintf(file, "%s\n", text);
fclose(file);
return 0; return 0;
} }
/* /*
@@ -6,15 +6,17 @@ int hidden_message(const char *filename, const char delim){
int c = fgetc(file); int c = fgetc(file);
printf("%c", c); if (c != EOF) putchar(c);
//char *cara = c; //char *cara = c;
while(c != EOF){ while(c != EOF){
c = fgetc(file); c = fgetc(file);
if( c == delim){ if( c == delim){
c = fgetc(file); while(c == delim) c = fgetc(file);
printf("%c", c);
if (c != EOF && c != delim)putchar(c);
} }
} }
fclose(file); fclose(file);
@@ -7,9 +7,13 @@ int print_lines(const char *filename){
// int c = fgetc(file); // int c = fgetc(file);
int c = fgetc(file); int c = fgetc(file);
printf("> ");
while(c != EOF){ while(c != EOF){
//if (c == '\n') printf("\n> ");
printf("%c", c); printf("%c", c);
if(c == '\n') printf("> ");
c = fgetc(file); c = fgetc(file);
/* /*
while(c != '\0'){ while(c != '\0'){
c = fgetc(file); c = fgetc(file);
@@ -18,11 +22,12 @@ int print_lines(const char *filename){
printf("\n"); printf("\n");
*/ */
} }
printf("\n");
return 0; return 0;
} }
/*
int main() int main()
{ {
print_lines("example.txt"); print_lines("example.txt");
@@ -37,4 +42,4 @@ int main()
return 0; return 0;
} }
*/
@@ -7,63 +7,11 @@ enum data_type {
int write_format(void *data, enum data_type type, char *filename) int write_format(void *data, enum data_type type, char *filename)
{ {
FILE *file = fopen(filename, "a"); FILE *file = fopen(filename, "w");
if(!file) return 1; if(!file) return 1;
printf("%i\n", type); fprintf(file, "test");
// FIXME: Implement this function
if(type == STRING){ fclose(file);
char *var = data;
fprintf(file, "String: %s\n", var);
}
else if (type == INTEGER){
int *var = data;
//printf("%c", *var % 10 + 48);
/*fprintf(file, "Integer: ");
while(*var > 10){
char tmp = *var % 10 + 48;
fprintf(file, "%c", tmp);
*var = *var / 10;
}
fprintf(file, "\n");*/
fprintf(file,"Integer: %d\n", *var);
}
else if (type == POINTER){
char *var = data;
fprintf(file, "Pointer: %p\n", var);
}
else return 1;
return 0; return 0;
} }
/*
int main()
{
char *str = "Write me!";
int integer = 42;
int *ptr = &integer;
//As you can see, we pass an integer pointer to the function.
//This is because genericity only applies to pointers, so you cannot directly pass an integer as a parameter.
//It also means that you will have to dereference it in order to retrieve its value.
write_format(str, STRING, "output.txt");
write_format(ptr, INTEGER, "output.txt");
write_format(ptr, POINTER, "output.txt");
// * output.txt should contain:
//String: Write me!
//Integer: 42
//Pointer: 0x424242424242 // Unfortunately, it may not be the result you will get, but something similar
// FLAG
//Complete the function in order to print the password into output.txt.
//
size_t password[2] = { 7940247895376159821, 0 };
write_format(password, STRING, "output.txt");
return 0;
}
*/