-2

I am running the following code based on this answer: C: read only last line of a file. No loops

 #include <arpa/inet.h>

 #include <netdb.h>
 #include <netinet/in.h>
 #include <unistd.h>
 #include <iostream>
 #include <cstring>  
 #include <stdlib.h>
 #include <stdio.h>

 main(){
 string answer= "Nothing, actually" 
 FILE* fd;
 if ((fd = fopen("result.txt", "r")) != NULL);
 static const long max_len = 55 + 1;
 char buf[max_len + 1];
 fseek(fd, -max_len, SEEK_END);
 ssize_t len = fread(fd, buf, max_len);
 buf[len] = '\0';
 char *last_newline = strrchr(buf, '\n');
 char *last_line = last_newline+1;
 answer= last_line;}

When I compile it via my makefile I get back:

tcp-server.cc:111:42: error: invalid conversion from ‘char*’ to ‘size_t {aka long unsigned int}’ [-fpermissive] ssize_t len = fread(fd, buf, max_len); ^ tcp-server.cc:111:42: error: too few arguments to function ‘size_t fread(void*, size_t, size_t, FILE*)’

I just want it to read the last line of a file. Have no idea why this is difficult or why I am getting the error. Edited to show update made after a comment that did fix one error, but not the current two.

Community
  • 1
  • 1
Kelly
  • 7
  • 4
  • 2
    use `fread` instead of `read`. the error will go away and everything else can stay the same – Raxvan Mar 31 '17 at 16:04
  • What have you tried in order to resolve the problem? Can you post a [SSCCE](http://sscce.org)? – Jesper Juhl Mar 31 '17 at 16:05
  • Raxvan now I'm getting: tcp-server.cc:111:42: error: invalid conversion from ‘char*’ to ‘size_t {aka long unsigned int}’ [-fpermissive] ssize_t len = fread(fd, buf, max_len); ^ tcp-server.cc:111:42: error: too few arguments to function ‘size_t fread(void*, size_t, size_t, FILE*)’ – Kelly Mar 31 '17 at 16:16
  • Jesper, as far as I can see that is SSCCE – Kelly Mar 31 '17 at 16:17
  • @Kelly no, it clearly is not since it is not compilable - for one, there's no `main`, there are also no `include`s for the types in use. Etc.. – Jesper Juhl Mar 31 '17 at 16:22
  • That help at all? – Kelly Mar 31 '17 at 16:27
  • `C++` programs are not "scripts", that's something different. Also if you indent and space your code others will be able to understand it better to help you. – Galik Mar 31 '17 at 16:37
  • I indent for when I change what I'm doing, as that's all one part of my script it's not indented. – Kelly Mar 31 '17 at 16:40
  • Also if it's not I don't know. I don't get this lanagague very well and everything on the internet just techno babbles to the point it's like reading german with every second word replaces with Chinese slang. – Kelly Mar 31 '17 at 16:41
  • @Kelly better.. although your `main` signature is incorrect - `main` *always* returns `int`. – Jesper Juhl Mar 31 '17 at 16:43
  • I don't even know what my "main signature" is :/ – Kelly Mar 31 '17 at 16:48

1 Answers1

1

You have numerous bugs. Here are a few/the first ones I've noticed:

"if ((fd = fopen("result.txt", "r")) != NULL);" makes no sense; you probably wanted to do something in case the condition was true/false, not just end the statement with ; there.

"static const long max_len = 55 + 1;" there's absolutely no reason for that static.

main always returns int - you can't have main with no return value.

std::fread takes 4 arguments but you are only passing it 3 (which btw is actually the error you mention in your question).

" string answer= ..." should be "std::string answer= ...".

Btw: this all looks like (pretty bad) C. Why are you tagging it as C++ (except for the use of string this has nothing to do with modern idiomatic C++)?

The best advice I can give you is: 1) turn on all compiler warnings and take them seriously. 2) buy and read a good C++ book.

Community
  • 1
  • 1
Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70