ReadTecxtFile

This commit is contained in:
2020-06-21 22:24:49 +02:00
parent 7f968cba8e
commit 04fbaa0cf5
5 changed files with 55 additions and 7 deletions

12
.theia/tasks.json Normal file
View File

@ -0,0 +1,12 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "runBuild",
"type": "shell",
"command": "./build_and_run.sh"
}
]
}

View File

@ -1,5 +1,5 @@
project(advanced_C_standard_library C)
set_property(GLOBAL PROPERTY C_STANDARD 11)
set_property(GLOBAL PROPERTY C_STANDARD)
cmake_minimum_required(VERSION 3.9)
include_directories(src)
file(GLOB SOURCES "src/*.c")
@ -8,3 +8,4 @@ find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
endif()
set( CMAKE_EXPORT_COMPILE_COMMANDS ON )

28
src/file.c Normal file
View File

@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
char* acl_ReadFileString(char *filePath, bool *sucess){
FILE *fp = fopen(filePath, "rb");
size_t lSize;
char *buffer;
if(!fp) {
*sucess = false;
return buffer;
}
fseek(fp, 0L, SEEK_END);
lSize = ftell(fp);
rewind(fp);
buffer = malloc(lSize + 1);
if(!buffer) {
fclose(fp);
*sucess = false;
return buffer;
}
size_t readReturn = fread(buffer, lSize, 1, fp);
fclose(fp);
if(readReturn != 1) {
*sucess = false;
}
*sucess = true;
return buffer;
}

5
src/file.h Normal file
View File

@ -0,0 +1,5 @@
#ifndef acl_file_h
#define acl_file_h
#include <stdbool.h>
char* acl_ReadFileString(char *filePath, bool *sucess);
#endif

View File

@ -1,11 +1,13 @@
#include <stdio.h>
#include "array.h"
#include <stddef.h>
#include "file.h"
#include <stdbool.h>
int main() {
int *int_array = acl_arraylist_create(2, sizeof (int));
int_array[0] = 28;
int_array[1] = 20;
for(int i = 0; i < 10; ++i) acl_arraylist_append(int_array, &i);
for(int i = 0; i < 12; ++i) printf("Index: %d Value: %d\n", i, int_array[i]);
bool sucess;
char *content = acl_ReadFileString("LICENSE", &sucess);
if(sucess) {
printf("%s", content);
}
else printf("Erorror\n");
}