Commit fa2172be authored by Matthew Burket's avatar Matthew Burket

First draft of the file operations

parent 659d2512
Pipeline #318 passed with stage
in 15 seconds
......@@ -3,15 +3,40 @@
//
#include "file_operations.h"
#include "string.h"
/// This method gets the last
///
/// \param fp
/// \param path
/// \param output
/// \return
int get_last_line(FILE fp, char **output) {
int get_last_line(char *path, char *output) {
char *currentLine = malloc(BUFFSIZE);
char *lastLine = malloc(BUFFSIZE);
FILE *fp = fopen(path, "r");
while (fgets(currentLine, BUFFSIZE, fp)) {
lastLine = strcpy(lastLine, currentLine);
}
strcpy(output, lastLine);
free(lastLine);
free(currentLine);
}
int get_specific_line(FILE fp, char **output) {
int get_specific_line(char *path, char *output, int finalLine) {
char *currentLine = malloc(BUFFSIZE);
char *lastLine = malloc(BUFFSIZE);
FILE *fp = fopen(path, "r");
int line = 1;
while (fgets(currentLine, BUFFSIZE, fp)) {
lastLine = strcpy(lastLine, currentLine);
if (line == finalLine) {
break;
}
line++;
}
strcpy(output, lastLine);
free(lastLine);
free(currentLine);
}
......@@ -6,7 +6,10 @@
#define COMS352_SHELL_FILE_OPERATIONS_H
#include <stdio.h>
#include <stdlib.h>
int get_last_line(FILE fp, char **output);
int get_specific_line(FILE fp, char **output);
#define BUFFSIZE (sizeof(char) * 5000)
int get_last_line(char *path, char *output);
int get_specific_line(char *path, char *output, int finalLine);
#endif //COMS352_SHELL_FILE_OPERATIONS_H
......@@ -5,10 +5,11 @@
#include <stdlib.h>
#include "tokenize.h"
#include "file_operations.h"
#define TRUE 0
#define READ 0
#define BUFFSIZE (sizeof(char) * 5000)
int main() {
// Setup Some Vars
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment