0

I have been trying for a long time to figure out how to get the program to read text from a file. I have tried a solution with fgets() and a loop. The program runs but does not print the variable, indicating the text was not extracted.

    #include <stdio.h>
    #include <string.h>
    #include "stdfn.h"


    int main(int argc, char* argv[])
    {
        char* request;
        char bf_text[1024];
        char character;

        intro("Site Blocker", 2014, "MIT");

        request = bash("curl http://redsec.ru/blocked_sites.txt"); // Source of bash() is at line 139 https://github.com/Pavelovich/lib-c/blob/master/stdfn.h
        //printf("%s", request);

        FILE* block_file = fopen(".blocked_sites", "w+"); // Changed from "w" based on this thread, currently only outputs a small part of the file.
        FILE* hosts = fopen("hosts", "w");
        FILE* hosts_tmp = fopen(".hosts", "w");

        // Print the text of the web request to the temporary
        // .blocked_sites file
        fprintf(block_file, "%s", request);

        rewind(block_file);
        fread(bf_text, sizeof(block_file), 1, block_file);
        printf("%s", bf_text);

        fclose(block_file);
        return 0;
    }
Lieutenant S.
  • 25
  • 1
  • 4

2 Answers2

3

sizeof(block_file) does not give you the size of the file. It'll give you the size of a file pointer, probably either four or eight bytes. Probably eight in your case, since you're saying it's reading "74.125.2", which is eight bytes, and then going haywire. You'll need to use something like stat() on a POSIX system, or a combination of fseek() and ftell().

You should also open files in binary mode if you're going to use fread() or fwrite(), since they are binary file IO functions. It won't make a difference on UNIX systems, but it may well on Windows, for instance. You shouldn't really mix text and binary mode IO functions in the way that you have for this reason.

You should also be checking the returns from your fopen() calls to make sure they succeeded.

And that bash() function you're using is completely broken, too. You'll get a memory leak every time it's called because it never free()s output, and it's making the same sizeof error that you are, although it'll still work because of the loop it's in. It'll just waste all that memory it allocated. And you are leaking memory because you never free(request). And you'd better never #include it in more than one translation unit, either, unless you want multiple definition errors all over the place. That whole "library" is riddled with schoolboy-type errors, in fact, including repeated failures to check the return from malloc(), allocating memory to fit a pointer instead of the thing it's pointing at, and so on.

Crowman
  • 25,242
  • 5
  • 48
  • 56
  • Yes! Thank you! This was correct. For anyone else running into the same problem, here's an example I used to see how to set up `fseek()` and `ftell()`. http://stackoverflow.com/a/238607 – Lieutenant S. Oct 09 '14 at 03:38
1

You are opening block_file for "write only". Try changing the mode parameter to "w+" i.e. FILE block_file = fopen(".blocked_sites", "w+"); If you want to open an existing file rather than creating a new one each time, use "r+" or "a+" instead of "w+".

  • Thank you for your answer. I just tried converting it to "w+" and it started to work. However, it only got as far as "74.125.2", which is part of the first IP address. It re-associates IP addresses and domain names to block malicious or hateful sites. – Lieutenant S. Oct 09 '14 at 01:28
  • I would need to see what your "bash" function does, although I would make an educated guess that it is embedding a null character in the data. printf and fprintf will only output a string up to the first null character. – user3354015 Oct 09 '14 at 01:33
  • The bash function is at line 139: https://github.com/Pavelovich/lib-c/blob/master/stdfn.h – Lieutenant S. Oct 09 '14 at 01:42