This is my a.c code :
#include <stdio.h>
#include <socket.h>
int main(void)
{
int count[4] = {[2] = 3 }, i;
for (i = 0; i < 4; i++)
printf("count[%d]=%d\n", i, count[i]);
return 0;
}
When I compile it, it shows:
a.c:2: fatal error: socket.h: No such file or directory
compilation terminated.
So how do I include it / where can download it?
It should be:
#include <sys/socket.h>
Paths are given relatively to the /usr/include path. So e.g. the socket.h file is under /usr/include/sys/socket.h. You can search for it if you don't know:
find /usr/include/ -name SEARCHED_HEADER.h
Related
I am trying to create a ppm file from inside a C program, but somehow it doesn't work.
It works fine when I am inside the IDE and run the program there. The program is executed and the file created inside the file's folder.
But once I build it and open the executable with a double click, the program runs in the terminal but does not create the file.
I am working on a mac and this is the relevant code, thanks in advance you all!
// from generalSettings.h
#define WIDTH 1100
#define HEIGHT 966
#define MYFILENAME "testimage.ppm"
int pictureArray[HEIGHT][WIDTH];
//from the main.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "generalSettings.h"
int main()
{
triangle firstParentTriangle;
// these two functions "draw" sierpinsky triangles in the pictureArray
drawFirstParentTriangle(&firstParentTriangle);
drawChildTriangles(firstParentTriangle, numberOfRecursions);
create_ppm();
return 0;
}
void create_ppm()
{
unsigned char color_black[] = "000 000 000\n";
unsigned char color_white[] = "255 255 255\n";
FILE *p_file = fopen(MYFILENAME, "w");
if (NULL != p_file)
{
fprintf(p_file, "P3\n %d %d\n 255\n", WIDTH, HEIGHT);
for (int i = 0; i < HEIGHT; i++)
for (int j = 0; j < WIDTH; j++)
if (1 == pictureArray[i][j])
fprintf(p_file, color_black);
else
fprintf(p_file, color_white);
fclose(p_file);
}
}
Here, I have replaced the foreground and background variables with local ones.
Generally, the program inserts 1 to the pictureArray[][] on certain elements and leaves others with a 0.
For my problem, this should be the relevant code for a reproducable example.
EDIT: Problem solver. File was created in the user folder due to missing path.
I need to write a simple program that prints the name of the most recently modified file whose name starts with 0-9 in the current directory. So far I can get it to print the name of a file that starts with 0-9, but it fails to consistently print the one that was modified most recently. I have been stuck at this part and I am in very desperate need to figure this out. Any help or hints would be of much help! Thank you!
Below is my code:
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
int main(void){
// Open the current directory
DIR* currDir = opendir("./");
struct dirent *aDir;
time_t lastModifTime;
struct stat dirStat;
int first_file_checked = 0;
char directoryName[256];
// Go through all the entries
while((aDir = readdir(currDir)) != NULL){
// only check on directories with a name that starts with 0-9
if (48 <= (int)aDir->d_name[0] && (int)aDir->d_name[0] <= 57) {
// Get meta-data for the current entry
stat(aDir->d_name, &dirStat);
// Use the difftime function to get the time difference between the current value of lastModifTime and the st_mtime value of the directory entry
if(first_file_checked == 0 || difftime(dirStat.st_mtime, lastModifTime) > 0){
lastModifTime = dirStat.st_mtime;
memset(directoryName, '\0', sizeof(directoryName));
strcpy(directoryName, aDir->d_name);
}
first_file_checked = 1;
}
}
// Close the directory
closedir(currDir);
printf("The last file/directory modified in the current directory is %s\n", directoryName);
return 0;
}
Works here:
BTW: you dont check for directories, you need to add a check for d_type to accomplish that.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
int main(void){
// Open the current directory
DIR* currDir ;
struct dirent *aDir;
time_t lastModifTime;
struct stat dirStat;
int count ;
char thename[256] = "";
// Go through all the entries
currDir = opendir("./");
for(count=0; (aDir = readdir(currDir)) ; ){
int rc;
// only check on directories with a name that starts with 0-9
if (aDir->d_name[0] < '0' || aDir->d_name[0] > '9' ) continue;
// Get meta-data for the current entry
rc = stat(aDir->d_name, &dirStat);
if (rc < 0) continue; // check the return value!
if(!count++ || dirStat.st_mtime < lastModifTime ){
lastModifTime = dirStat.st_mtime;
strcpy(thename, aDir->d_name);
}
}
// Close the directory
closedir(currDir);
if (count) {
printf("%d files checked. The last file/directory modified in the current directory is %s(time=%u)\n"
, count, thename, (unsigned) lastModifTime);
}
else {
printf("No files/directories were found\n");
}
return 0;
}
New to programming, actually following an open classroom tutorial on C and I'm finding myself stuck on that including external header part. I use an IDE, I'm on Visual Studio Code, I could switch to an IDE but it should be possible and not to difficult to do it in a simple text editor so I want to understand how.
The error code when running the program:
main.c:6:10: fatal error: 'level.h' file not found
#include "level.h"
^~~~~~~~~
1 error generated.
Here's my main.c code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <string.h>
#include "level.h"
#include "menu.h"
int level();
int menu();
int main()
{
int nombreMystere, guess, round = 10;
char answer;
bool exit = true;
while (exit)
{
menu();
{
while(guess != nombreMystere)
{
printf("%d" , nombreMystere);
printf("\nIl vous restes %d round.\n", round );
printf("Trouver le nombre magique: " );
scanf ("%d" , &guess );
if(guess < nombreMystere) printf("Trop bas\n");
else if(guess > nombreMystere) printf("Trop Haut\n");
round --;
if (round == 0)
{
printf("You Lose.. Dumbass");
break;
}
if (guess == nombreMystere) printf("\nGood job !\n");
}
printf("Play again?\n (yes/no): ");
scanf("%s", &answer);
if (answer == 110 || answer == 78) {exit=0;}
}
}
return 0;
}
Here's level.c:
static int selec;
int level(int x)
{
int max, difficulty = selec;
if (difficulty==1){max = 100 ;}
else if (difficulty==2){max = 1000 ;}
else if (difficulty==3){max = 10000 ;}
return max;
}
level.h:
int level(int x);
menu.c:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "menu.h"
int level();
int menu()
{
srand(time(NULL));
int max, nombreMystere, selec;
const int MIN = 1;
while (selec < 1 || selec > 3)
{
printf("\n\nSelect Difficulty level:\n");
printf("1: Level 1\n");
printf("2: Level 2\n");
printf("3: Level 3\n");
scanf("%d", &selec);
level(selec);
printf("\n\n%d\n\n", max);
//if (difficulty==1){max = 100 ;}
//else if (difficulty==2){max = 1000 ;}
//else if (difficulty==3){max = 10000 ;}
//else {printf("Wrong selection, please try again.");}
nombreMystere = (rand() % (max - MIN + 1)) + MIN;
}
return nombreMystere;
}
menu.h:
int menu();
I've been searching the whole day on the internet a way to fix this issue but I don't understand half of what I'm seeing and every topic was talking only about C++. I've seen somewhere that it could come from the tasks.json file in VSCode but it was on a C++ topic.
I'm using macOS.
Thanks for your time.
EDIT: Both .h and .c files are in separate folders and both folders are in the same directory.
The level.h file needs to be in the SAME folder as main.c.
Also the menu.h file should be in the same folder. So make sure you have all the files in the same folder.
Additionally these two lines shouldn't be in your main.c:
int level();
int menu();
Since this is essentially what including the headers does for you. On that note I also can't see you calling the function level() in your main.c file, thus you do not even need to include the level.h file at all. However you do call the function menu() so you do need to #include "menu.h" like you are doing.
Also in the menu.c file you don't need this line #include "menu.h", you should change it to #include "level.h" since you are calling the level() function in your code and then also remove the line after it, that is; the line int level();, since that's essentially what including the level header will do for you.
The level.c file looks fine to me.
If you're still getting an error with all the files in the same folder, make sure you are compiling it correctly.
In include "x" the value of x represents the path to your header file (relative using the directories . and .. in your current directory or absolute paths, the latter is a BAD PRACTICE don't do it, but you can, side note: the ~ is not supported) if all your files are in the same directory you shouldn't have such an error, you don't have to use the relative path it assumes the current directory by default, but if they aren't consider including them the right way. see sveinnth's answer for more notes about your coding style.
one more thing, you should include your compilation options in your question.
I try to import some C-function that generates an array in SystemVerilog.
Here is code:
#include "svdpi.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void getPacket(int packetSize, svOpenArrayHandle fpSig, svOpenArrayHandle err)
{
int* cfpSig = (int*) calloc(packetSize, sizeof(int));
double* cerr = (double*)calloc(packetSize, sizeof(double));
for(int i = 0; i < packetSize; ++i)
{
cfpSig[i] = i;
cerr[i] = 1.1*i;
printf("%d %f\n",cfpSig[i],cerr[i]);
}
printf("----------");
memcpy((int*) svGetArrayPtr(fpSig),cfpSig,packetSize);
memcpy((int*) svGetArrayPtr(err),cerr,packetSize);
free(cfpSig);
free(cerr);
}
import "DPI-C" function void getPacket(input int packetSize,
output int fpSig[], output real err[]);
module top();
initial begin
parameter int packetSize = 4;
int fpSig[packetSize];
real err[packetSize];
getPacket(packetSize,fpSig,err);
for(int i = 0; i < packetSize; ++i) begin
$display("fpSig: %d\nerr : %f",fpSig[i],err[i]);
end
end
endmodule
But when I compile the c-code manually, an error is generated at the linking stage: undefined reference to 'svGetArrayPtr'.
I have not previously worked with svOpenArrayHandle and it was enough to connect the header file "svdpi.h". I tried to look for some svdpi.dll lib in the questa install folder, but didn't find it.
If I compile c-file by vlog it's working fine, but I want to compile it manually because I plan to include matlab libs and compiling via vlog will become uncomfortable.
In Questasim simulator, the library containing the svGetArrayPtr symbol is mtipli.dll
I am completing cs50x (the edX (free) version of the Harvard cs50) course and am trying to be a bit tricky/lazy/test myself.
I am trying to use a C program to create all the directories I will need for my psets.
I have looked online and found that <sys/stat.h> includes the mkdir() function and therefore tried creating some nested loops to create all the necessary folders by doing something similar to mkdir {pset1,pset1/{standard,hacker},pset2,pset2{standard... to give me a directory structure like this:
pset1/Standard
pset1/Hacker
pset2/Standard
etc...
I came up with this:
#include <cs50.h>
#include <stdio.h>
#include <sys/stat.h>
int main(int argc, string argv[])
{
for(int i = 1; i <=8; i++)
{
string dir = argv[1];
sprintf(dir,"%s%i", argv[1], i);
mkdir(dir, 0777);
for(int j = 0; j<2; j++)
{
string subDir[] = {"Standard","Hacker"};
sprintf(dir,"%s%i/%s", argv[1], i, subDir[j]);
mkdir(dir, 0777);
}
}
}
However, the program only creates pset1 and completes, there are no subfolders, no pset2 etc.
Yes, you're being lazy since you seem to have very little knowledge of C, yet try to program in it. :)
C is not Python, there is no string interpolation/formatting operator. You have to call a function, specificially snprintf(). Read that manual page.
Also, you can't create a bunch of nested directories with a single call to mkdir(). Read the manual page.
To create nested directories, you're either going to have to build each's absolute path (i.e. each successive time you call mkdir() the path will be longer than the previous time), or actually enter each directory as you create it, and go from there.
To create a full path you can call mkdir() recursivly like this:
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
int mkdirr(const char * path, const mode_t mode, const int fail_on_exist)
{
int result = 0;
char * dir = NULL;
do
{
if (NULL == path)
{
errno = EINVAL;
result = -1;
break;
}
if ((dir = strrchr(path, '/')))
{
*dir = '\0';
result = mkdirr(path, mode, fail_on_exist);
*dir = '/';
if (result)
{
break;
}
}
if (strlen(path))
{
if ((result = mkdir(path, mode)))
{
char s[PATH_MAX];
sprintf(s, "mkdir() failed for '%s'", path);
perror(s);
if ((EEXIST == result) && (0 == fail_on_exist))
{
result = 0;
}
else
{
break;
}
}
}
} while (0);
return result;
}
And then call mkdirr() like this;
int main(void)
{
char p[] = "test/1/2/3";
if (-1 == mkdirr(p, 0777, 0))
{
perror("mkdirr() failed()");
}
return 0;
}