Last active
October 15, 2020 18:53
-
-
Save aleperaltabazas/6de0699fd35a3d1de9c17d8b526b2107 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <stdlib.h> | |
| #include <stdio.h> | |
| typedef struct { | |
| int longitud; | |
| char *texto; | |
| } t_texto; | |
| int length(char* str) { | |
| int length = 0; | |
| while (str[length] != '\0') { | |
| length++; | |
| } | |
| return length; | |
| } | |
| char* strcat(char* first, char* second) { | |
| char* total = malloc(sizeof(char) * (length(first) + length(second)) + 1); | |
| int i = 0; | |
| for (; first[i] != '\0'; i++) { | |
| total[i] = first[i]; | |
| } | |
| for (int j = 0; second[j] != 0; i++, j++) { | |
| total[i] = second[j]; | |
| } | |
| total[i] = '\0'; | |
| return total; | |
| } | |
| char *inputString(FILE *fp) { | |
| //The size is extended by the input with the value of the provisional | |
| char *str; | |
| int ch; | |
| size_t len = 0; | |
| size_t size = 1; | |
| str = realloc(NULL, sizeof(char) * size);//size is start size | |
| if (!str)return str; | |
| while (EOF != (ch = fgetc(fp)) && ch != '\n') { | |
| str[len++] = ch; | |
| if (len == size) { | |
| str = realloc(str, sizeof(char) * (size += 1)); | |
| if (!str)return str; | |
| } | |
| } | |
| str[len++] = '\0'; | |
| return realloc(str, sizeof(char) * len); | |
| } | |
| t_texto cargarTexto() { | |
| char* input = inputString(stdin); | |
| int len = length(input); | |
| t_texto texto = { | |
| .texto = input, | |
| .longitud = len | |
| }; | |
| return texto; | |
| } | |
| void escribirArchivo(char* destination, t_texto texto) { | |
| FILE* fp = fopen(destination, "w"); | |
| char* n = malloc(sizeof(char) * 16); | |
| sprintf(n, "%i", texto.longitud); | |
| char * string = strcat(n, ","); | |
| char * string_posta = strcat(string, texto.texto); | |
| fwrite(string_posta, sizeof(char), sizeof(texto), fp); | |
| fclose(fp); | |
| free(string); | |
| free(string_posta); | |
| } | |
| int main() { | |
| t_texto txt = cargarTexto(); | |
| escribirArchivo("/home/alejandroperalta/textos.csv", txt); | |
| free(txt.texto); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment