0

I have a task with this code

#include <stdlib.h>
#include <string.h>
int writeLog(char const *text)
{
 char *auxText = strdup(text);
 if(auxText==NULL)
 return -1;

 char *ptr = auxText + strlen(text);

 do{
 --ptr;
 if(*ptr=='\n' || *ptr=='\r')
 *ptr = 0;
 } while(ptr!=auxText);
 if(strlen(auxText)==0)
 {
 free(auxText);
 return -1;
 }

 FILE *fp = fopen("log.txt","a");
 if(fp==NULL)
 {
 free(auxText);
 return -1;
 }

 fprintf(fp, "%s\n", auxText);
 free(auxText);
 return 0;
}

this is the utils.c file

and the main.c flile:


#include <stdio.h>
#include "utils.h"
int main(int argc, char *argv[]) {
 writeLog("teste");

 return 0;
}
  

but when I do: $ cc main.c utils.c -o myapp

give me this error:

enter image description here

mpromonet
  • 11,326
  • 43
  • 62
  • 91
  • You have shown a utils.c file. You did not show a utils.h file? Do you have one? Where in relation to the .c file with the `#inlcude` is it located? What is the effect you expect from the line `#include "utils.h"`? – Yunnosch Apr 26 '21 at 20:20

2 Answers2

0

You should create a file utils.h in the same directory, with the following contents:

#pragma once
int writeLog(const char* text);

Your problem is main.c not finding this file - #include is trying to open the file and paste it’s contents into main.c

  • 1
    [#pragma once is a non-standard pragma](https://en.cppreference.com/w/cpp/preprocessor/impl) and see [What does #pragma once mean in C?](https://stackoverflow.com/questions/5776910/what-does-pragma-once-mean-in-c) – David C. Rankin Apr 26 '21 at 21:06
  • It’s working as protection against re-definition, just like #ifndef MY_FILE_H ... That’s why I put it there, it should work on modern compilers - I’ve seen them work in GCC and Cmake, can’t test MSVC though.. –  Apr 27 '21 at 04:55
-1

You must have the prototype for the writeLog in utils.h.

ADBeveridge
  • 650
  • 3
  • 15