Add a file getline() function to parse files one line at a time

This commit is contained in:
baldurk
2016-08-01 22:29:28 +02:00
parent e8cb71a895
commit 9dbb37a0cd
3 changed files with 42 additions and 0 deletions
+2
View File
@@ -242,6 +242,8 @@ FILE *fopen(const char *filename, const char *mode);
size_t fread(void *buf, size_t elementSize, size_t count, FILE *f);
size_t fwrite(const void *buf, size_t elementSize, size_t count, FILE *f);
std::string getline(FILE *f);
uint64_t ftell64(FILE *f);
void fseek64(FILE *f, uint64_t offset, int origin);
+20
View File
@@ -249,6 +249,26 @@ FILE *fopen(const char *filename, const char *mode)
return ::fopen(filename, mode);
}
string getline(FILE *f)
{
string ret;
while(!FileIO::feof(f))
{
char c = (char)::fgetc(f);
if(FileIO::feof(f))
break;
if(c != 0 && c != '\n')
ret.push_back(c);
else
break;
}
return ret;
}
size_t fread(void *buf, size_t elementSize, size_t count, FILE *f)
{
return ::fread(buf, elementSize, count, f);
+20
View File
@@ -343,6 +343,26 @@ FILE *fopen(const char *filename, const char *mode)
return ret;
}
string getline(FILE *f)
{
string ret;
while(!FileIO::feof(f))
{
char c = (char)::fgetc(f);
if(FileIO::feof(f))
break;
if(c != 0 && c != '\n')
ret.push_back(c);
else
break;
}
return ret;
}
size_t fread(void *buf, size_t elementSize, size_t count, FILE *f)
{
return ::fread(buf, elementSize, count, f);