c

C: strtok delivers segmentation fault

跟風遠走 提交于 2021-02-17 06:38:06
问题 I am trying to read a file line by line, and tokenize each line, which have strings separated by spaces and tabs. However, when I run my program, I get the a Segmentation Fault error when I try to print the token. I don't understand why this is happening, as I am using a buffer as the string to tokenize and checking if the token is null. Below is my code: #include <stdio.h> #include <stdlib.h> #define MAX_LINE_LENGTH 70 int main(void) { FILE * testFile; char buf[MAX_LINE_LENGTH]; testFile =

Calculate Euclidean distance matrix in C

本小妞迷上赌 提交于 2021-02-17 06:37:27
问题 I would like to convert this code which is written in MATLAB to C: matrix = [1 2 3; 4 5 6; 7 8 10] dis=zeros(9); for i=1:3 for j=1:3 dis(i,j)=sqrt(sum (abs((matrix(i,:)-matrix(j,:))))^2); end end The output is as follows: 0 9 19 9 0 10 19 10 0 Here is what I came up with in C: #include <stdio.h> #include <math.h> int main() { double distance[3][3] = {0}; double myArray[3][3] = { {1, 2, 3}, {4 , 5, 6}, {7, 8, 9} }; int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { distance[i][j] =

C error: “Undefined reference to 'sf_open'” from libsndfile

非 Y 不嫁゛ 提交于 2021-02-17 06:29:42
问题 I am using Code::Blocks to test some codes, and I just can't biuld this code: #include <stdio.h> #include <stdlib.h> #include <sndfile.h> int main() { SNDFILE *sf; sf = sf_open("file.wav",SFM_READ,&info); //error happens in this line ... And the error is "undefined reference to `sf_open'", this not only happens to sf_open but also to any function from sndfile.h. Actually here is what I did to get here: I've first downloaded and installed libsndfile for Windows x64 (I'm using Windows 10) from

C How to find and group coordinates located in a grid

左心房为你撑大大i 提交于 2021-02-17 06:29:07
问题 I have a C program where a CSV file containing 8 x,y coordinates are inserted into a linked list. Each of the 8 coordinates belongs in a 2x2 grid. There are 4 grids as seen in the picture below: First I want my program to determine which coordinate belongs in which grid. Once I've determined that, for each grid, I want to sum all of the x-coordinates and y-coordinates. Then for each grid 1-4, I want to then print the total sum of the x coord and y coord. #include <stdio.h> #include <stdlib.h>

Why does the Linux Open system call not need a buffer size parameter for the path?

柔情痞子 提交于 2021-02-17 06:25:26
问题 Why does the open system call not need a buffer size parameter like the write system call does? How do these two system calls treat their string parameters differently? Does the open system call assume a zero-terminated string for the path parameter while the write system call does not? If so why the inconsistency? Why not make all (or none) of the system calls that use strings / arrays require a size parameter? 回答1: UNIX was developed as an operating system for programs written in assembly,

Exiting out of file with feof function

倖福魔咒の 提交于 2021-02-17 06:25:07
问题 I am trying to make a photo extraction program stop when it detects it is at the end of the file to be extracted. I did this by placing an if condition: if (feof(file)) { return 2; } After a fread function: fread(array, 1, 512, file); So that if fread reads to the end of the file, then feof will trigger and thereby end the program. This is my code: #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main(int argc, char *argv[]) { if (argc != 2) { printf("Invalid entry.\n"); return

How to read data from file into two dimension array in C?

不羁岁月 提交于 2021-02-17 06:23:26
问题 I'm trying to read from a file and print out what's in the file. Data in file is something like the following and consist of 100 rows and 10 columns. -1,0.53,1,1,1,0,0.8,1,0.5,0 Here is what I have tried: #include <stdio.h> #include <stdlib.h> #define NUMBEROFINPUT 100 //100 inputs. #define NUMBEROFCOLUMN 10 //10 rows. int main(){ int *dataFileInput[NUMBEROFINPUT][NUMBEROFCOLUMN]; FILE *dataFileptr; dataFileptr = fopen("group5_8.txt", "r"); if (dataFileptr == NULL){ perror("Error"); return 1;

Concatenate and stringize macro values for #include

旧街凉风 提交于 2021-02-17 06:22:07
问题 I'm trying to create a string from multiple macros/values for use in a #include . I'm doing this to clean up some code for an initial state in a simple state system. I have 2 default, redefinable macros (if not defined there's a default value) #define DEFAULT_STATE StateName // name of class #define DEFAULT_STATE_LOCATION states/ // location of header file from root The include directive is being used from a file 4 folders in from the root , so the include should look like this #include "../.

thread_fork working on kernel

只谈情不闲聊 提交于 2021-02-17 06:09:02
问题 I am working on OS161 where C pthread library is not primarily supported. My current objective is to understand the sys calls and make some simple programs run. my simple function has following code: int id = 1; long id2 = 1; int ret = thread_fork("myThread", (void *)id, id2, void (*function)((void *)id, id2), NULL); kprintf("\nHello World\n"); return; ` where call to thread_fork is int thread_fork(const char *name, void *data1, unsigned long data2, void (*func)(void *, unsigned long), struct

thread_fork working on kernel

会有一股神秘感。 提交于 2021-02-17 06:07:56
问题 I am working on OS161 where C pthread library is not primarily supported. My current objective is to understand the sys calls and make some simple programs run. my simple function has following code: int id = 1; long id2 = 1; int ret = thread_fork("myThread", (void *)id, id2, void (*function)((void *)id, id2), NULL); kprintf("\nHello World\n"); return; ` where call to thread_fork is int thread_fork(const char *name, void *data1, unsigned long data2, void (*func)(void *, unsigned long), struct