-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReader.hpp
More file actions
99 lines (74 loc) · 2.12 KB
/
FileReader.hpp
File metadata and controls
99 lines (74 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// Created by Walfred (Wangfei) MA at the University of Southern California,
// Mark Chaisson Lab on 2/13/23.
//
// Licensed under the MIT License.
// If you use this code, please cite our work.
//
#ifndef FileReader_hpp
#define FileReader_hpp
#include <stdio.h>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <utility>
#include <cstring>
#include "config.hpp"
using namespace std;
class FileReader
{
public:
const char *filepath;
int num_seq = 0;
std::vector<spair> seqs;
FileReader(const char* inputfile, int index = -1):filepath(inputfile) {};
virtual bool nextLine(std::string &StrLine)=0;
virtual void Load()=0;
virtual void Close()=0;
int GetLine(FILE* fptr, std::string &line) {
char buf[1024];
line = "";
while (true) {
if (fgets(buf, sizeof(buf)-1, fptr) == nullptr) {
// fgets failed, check if it's an error or end-of-file
if (feof(fptr)) {
// End-of-file reached
break;
} else {
// I/O error occurred
perror("Error reading from file");
return -1;
}
}
line += buf;
if (line.back() == '\n') {
// Remove the newline character from the end of the line
line.pop_back();
break;
}
// If there are no more characters in the file, break the loop
if (feof(fptr)) {
break;
}
}
// Return 1 if the file has more lines, 0 if it's the last line, -1 if an error occurred
return !feof(fptr);
}
/*int GetLine(FILE* fptr, string &line) {
char buf[1025];
line="";
int res;
while ((res=fscanf(fptr, "%1024[^\n]", buf))) {
if (res) {
line+=buf;
}
else {
break;
}
}
char c = fgetc(fptr);
return (!feof(fptr));
} */
};
#endif /* FileReader_hpp */