0

I have a practice question in C that asks me to create a function that only copies part of a file to another one. The restrictions are that lines with greater than maxlen characters are not copied to the new file, and the newline character does not count, so it should not be copied. Part of my function says that if a file does not exist, it should explicitly say so, and I am getting those error messages when I run that code; however, I can see that the files are created are inside my folder. Whenever I open the file I'm trying to read after running the code, I get this:

./Debug/main.c.o ./Debug/dot.c.o ./Debug/dataBase.c.o ./Debug/intPrompt.c.o ./Debug/numWords.c.o ./Debug/LinkedList.c.o   

Below is my code :

void shortLines(char* f1, char* f2, int maxlen) {
    FILE* fp = fopen(f1, "r");
    FILE* fp2 = fopen(f2, "w");
    if (fp == NULL) {
        perror("File does not exist");
    }
    if (fp2 == NULL) {
        perror("File does not exist");
    }
    char singleLine[maxlen];
    char check;
    size_t len;
    do {
        fgets(singleLine, maxlen, fp);
        len = strlen(singleLine);
        if (singleLine[len-1] == '\n') {
            singleLine[len-1] = '\0';
        }
        fprintf(fp2, "%s", singleLine);
    } while ((check=getc(fp) != EOF));
}

int main(int argc, char **argv) {
    shortLines("Andrew.txt", "Andrew2.txt", 25);
    return 0;
}
DrJessop
  • 462
  • 6
  • 26
  • If you are on POSIX (or some other system with `getline`) better use [getline](http://linux.die.net/man//3/getline) like [here](https://stackoverflow.com/a/9171511/841108). BTW, you'll better use `calloc` for `char*singleLine;` – Basile Starynkevitch Nov 21 '17 at 17:35
  • 2
    Why do you print out an error message when the file doesn't exist, but then continue on to try and read/write to them? – Chris Turner Nov 21 '17 at 17:37
  • @Chris Turner The files do exist, which is why I'm confused I'm getting error messages. – DrJessop Nov 21 '17 at 17:42
  • 1
    They may exist, but for whatever reason, your code cannot open them so continuing on when `fp` is `NULL` is futile. – Chris Turner Nov 21 '17 at 17:44
  • @ringø My file contains random lines of varying lengths – DrJessop Nov 21 '17 at 17:50
  • @ringø Sorry, you are totally right, I just edited my question. The output on the file was on one line. And the main was just a call to this function. – DrJessop Nov 21 '17 at 17:56
  • @ringø Alright, it's there now – DrJessop Nov 21 '17 at 18:06
  • Removed some comments.. 1) fgets should not read maxlen chars max - longer lines will be read in 2+ times => read in a bigger buffer (at least the largest line size+1), then check length to ensure it's not bigger than maxlen to save the line. 2) On Linux (usually) file names are case sensitive - is your file exactly Andrew.txt? 3) Check its content - again. – Déjà vu Nov 21 '17 at 18:29
  • 1
    Instead of `perror("file does not exist")`, you should just write `perror(f1)`. It is entirely possible that the problem is not non-existence, and an error message of the form "file does not exist: permission denied" is just plain confusing. – William Pursell Nov 21 '17 at 18:47

1 Answers1

0

I just made new files called Andrew.txt and Andrew2.txt and these ones seem to be working for some strange reason. Regardless, there were a few problems in the code. First of all, after fgets is called, I needed to make sure to flush out the remaining characters in the line. I do this with a while loop and fgetc. If I reach an EOF, then I continue, and then fgets also returns an EOF, thus breaking the outer loop.

void shortLines(char* f1, char* f2, int maxlen) {
    FILE* fp = fopen(f1, "r");
    FILE* fp2 = fopen(f2, "w");
    if (fp == NULL) {
        perror(f1);
    }
    if (fp2 == NULL) {
        perror(f2);
    }
    char line[maxlen+1];
    size_t len;
    char c;
    while (fgets(line, maxlen+1, fp) != NULL) {
        len = strlen(line);
        if (len == maxlen) {
            while ((c=fgetc(fp)) != '\n') {
                if (feof(fp)) {
                    break;
                }
            }
            continue;
        }
        if (line[len-1] == '\n') {
            line[len-1] = '\0';
        }
        fprintf(fp2, "%s\n", line);
    }
}
DrJessop
  • 462
  • 6
  • 26