Last active
November 26, 2021 12:28
-
-
Save geo-stanciu/2782585 to your computer and use it in GitHub Desktop.
[C] Read file line by line
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
/* | |
Copyright (c) 2012, Gheorghita Stanciu gheorghita(dot)stanciu(at)gmail(dot)com | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
The views and conclusions contained in the software and documentation are those | |
of the authors and should not be interpreted as representing official policies, | |
either expressed or implied, of the FreeBSD Project. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <errno.h> | |
char * read_line (FILE *file) | |
{ | |
const int BUFFER_LENGTH = 1024; | |
char buffer[BUFFER_LENGTH]; | |
char *result = 0; | |
int length = 0; | |
int len = 0; | |
size_t result_len = 0; | |
while (!feof (file)) { | |
if (!fgets (buffer, BUFFER_LENGTH, file)) { | |
return result; | |
} | |
len = strlen (buffer); | |
length += len; | |
char *tmp = (char *) malloc ((length + 1) * sizeof (char)); | |
if (!tmp) { | |
printf ("error while trying to allocate %ld bytes. \n", | |
(length + 1) * sizeof (char)); | |
return result; | |
} | |
tmp[0] = '\0'; | |
if (result) { | |
strcpy (tmp, result); | |
free (result); | |
result = tmp; | |
} else { | |
result = tmp; | |
} | |
strcat (result, buffer); | |
if (strstr (buffer, "\n")) | |
break; | |
} | |
if (result) { | |
result_len = strlen(result); | |
if (result_len > 1 && result[result_len - 2] == '\r') { | |
result[result_len - 2] = '\0'; | |
} else if (result_len > 0) { | |
if (result[result_len - 1] == '\n' | |
|| result[result_len - 1] == '\r') { | |
result[result_len - 1] = '\0'; | |
} | |
} | |
} | |
return result; | |
} | |
char * get_next_token (const char *line, | |
const char *delimiters, | |
size_t *current_position) | |
{ | |
char *token = NULL; | |
const char *start_token = NULL; | |
const char *end_token = NULL; | |
size_t len = 0; | |
if (!line) | |
return token; | |
if (*current_position >= strlen(line)) | |
return token; | |
if (*current_position > 0) { | |
start_token = line + *current_position; | |
} else { | |
start_token = line; | |
} | |
if (!delimiters) { | |
token = (char *) malloc (strlen(start_token) * sizeof (char)); | |
strcpy(token, start_token); | |
*current_position += strlen (token) + 1; | |
return token; | |
} | |
end_token = strpbrk(start_token, delimiters); | |
if (!end_token) { | |
token = (char *) malloc (strlen(start_token) * sizeof (char)); | |
strcpy(token, start_token); | |
*current_position += strlen (token) + 1; | |
return token; | |
} else { | |
len = end_token - start_token; | |
token = (char *) malloc ((len + 1) * sizeof (char)); | |
strncpy(token, start_token, len); | |
token[len] = '\0'; | |
*current_position += strlen (token) + 1; | |
} | |
return token; | |
} | |
int main (int argc, char **argv) | |
{ | |
char *line = NULL; | |
char *token = NULL; | |
int line_no = 0; | |
size_t cur_pos = 0; | |
FILE *f; | |
if (argc != 2) { | |
printf ("Wrong argumets. Usage: ReadLines.exe <filename>.\n"); | |
return 1; | |
} | |
f = fopen (argv[1], "r"); | |
if (!f) { | |
printf ("Can not open file: %s. %s", argv[1], strerror(errno)); | |
return 1; | |
} | |
while ((line = read_line (f)) != NULL) { | |
printf ("line #%d: \"%s\"\n", line_no + 1, line); | |
cur_pos = 0; | |
while ((token = get_next_token (line, "\t", &cur_pos)) != NULL) { | |
printf ("token \"%s\"\n", token); | |
free (token); | |
token = NULL; | |
} | |
free (line); | |
line = NULL; | |
line_no++; | |
} | |
fclose (f); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! I was writing my own function but yours helped me more than mine. Great code.