Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
My env is:
system:ubuntu 18.04
gcc version 7.5.0
memory:
KiB Mem : 7930352 total, 5953392 free, 1241908 used, 735052 buff/cache
KiB Swap: 5169144 total, 5169144 free, 0 used. 6419340 avail Mem
The fpp always gets NULL point, there is the code below:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main(void)
{
DIR *dirp;
char** fpp=NULL;
const char* basePath="/tmp/temp";
if ((dirp=opendir(basePath)) == NULL)
{
perror("Open dir error...");
exit(1);
}
fpp = (char**)malloc(8);
if (NULL == fpp);
{
printf("error,no mem for fpp\r\n");
}
if (NULL != fpp)
{
free(fpp);
fpp=NULL;
}
if (NULL != dirp)
closedir(dirp);
return 0;
}
You have
if (NULL == fpp);
{
printf("error,no mem for fpp\r\n");
}
which is equivalent to
if (NULL == fpp)
{
}
{
printf("error,no mem for fpp\r\n");
}
Remove the extraneous semi-colon.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have a pretty simple program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
// argc is amount of elements that the user inputs, check if 1234 isn't in code
if (strcmp(argv[argc-1],"1234" != 0)) {
exit(-1);
}
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i],"-h")) {
// do whatever
printf("Hello world!");
} else if (strcmp(argv[i],"-f")) {
// argv[i+1] will be the file, print out the file to console
}
}
}
It allows the user to enter something, say ./my_kill -h 1234. If 1234 isn't the last thing, it's supposed to just exit. Then in the for loop, if -h is used it prints hello world. For some reason it is giving me a segmentation fault and I don't understand why.
You have a parenthesis in the wrong position.
if (strcmp(argv[argc-1],"1234" != 0))
should be
if (strcmp(argv[argc-1],"1234") != 0)
Also, the first thing you should do is check if there is any information inside of argv.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This is my program:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a, b, i;
scanf("%d\n%d", &a, &b);
// Complete the code.
for(i=a;i<=b;i++)
{
if (i=1)
{
printf("one");
}
else if(i=2)
{
printf("two");
}
else if(i=3)
{
printf("three");
}
.
.
.
}
}
return 0;
}
it goes till 10.
Even if a=8 the i=1 condition is executed and doesn't terminate.
The output is oneoneoneoneoneoneoneoneone.....
To compare values, do :
if (i == 1)
With what you did :
if (i = 1)
You assign 1 to i and the if condition check if it succeded, which is true.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I created a file called "text.txt" with a string inside and I want to copy that string in another file called "copiaqui.txt". But there's a problem. In the output file, I found this :
Why the program doesn't copy the string correctly?
Code :
#include <stdio.h>
#include <stdlib.h>
void copiaFile(FILE *fi, FILE *fo);
int main(void)
{
FILE *fi = fopen("test.txt", "r");
FILE *fo = fopen("copiaqui.txt","w");
if (fi == NULL)
{
printf("\nImpossibile aprire il file test.txt\n");
exit(EXIT_FAILURE);
}
if (fo == NULL)
{
printf("\nImpossibile aprire il file copiaqui.txt\n");
exit(EXIT_FAILURE);
}
copiaFile(fi, fo);
fclose(fi);
fclose(fo);
return 0;
}
void copiaFile(FILE *fi, FILE *fo)
{
int var;
while((var = getc(fi) != EOF))
{
printf("\nCarattere acquisisto : %c", var);
putc(var, fo);
}
}
You have made a common mistake with this expression:
var = getc(fi) != EOF
What this does is assign the value of (getc(fi) != EOF) to var, because of something called operator precedence. The value is either true or false. What you intended to do is:
(var = getc(fi)) != EOF
Which will make var have the getc() value, then check that against EOF.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
This program recieves a pointer to a const char* data type, loads a text file from disk into memory, and passes the address of the first index of the resulting char[] back (essentially passes the contents of the file back as a 'string').
This works just fine, though it sometimes passes back a few extra characters with the file contents.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char* loadShaders(char* PATH) {
FILE *fp = fopen(PATH, "rb");
if (fp == NULL) {
perror("[ctb.h] loadShaders() ");
printf("[ctb.h] loadShaders() recieved file path: %s\n", PATH);
exit(-1);
}
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
rewind(fp);
char* shader = malloc(fsize + 1);
fread(shader, fsize, 1, fp);
shader[fsize + 1] = '\0';
fclose(fp);
return shader;
}
When it does pass extra characters back, the result looks something like this:
#version 330 core
layout (location = 0) in vec3 aPos;
void main() {
gl_Position = vec4(aPos, 1.0);
}�
As you may have guessed, the "�" does not belong.
Suggestions?
This line causes undefined behavior by writing off the end of an array:
shader[fsize + 1] = '\0';
Simple to fix:
shader[fsize] = '\0';
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
When using the following routine to copy files, I'm getting an "Illegal seek" error. What could cause that? they are both ordinary files.
bool copyfile(const char* src, constchar* dest, bool overwrite_existing)
{
if (!overwrite_existing && file_exists(dest_filename))
return false;
int read_fd;
int write_fd;
struct stat stat_buf;
off_t offset = 0;
printf("src_filename=%s\n", src_filename.str());
if (read_fd = open(src_filename, O_RDONLY) == -1)
return false;
if (fstat(read_fd, &stat_buf) == -1){
perror("fstat\n");
return false;
}
write_fd = open(dest_filename, O_WRONLY | O_CREAT | O_TRUNC, stat_buf.st_mode);
if (write_fd == -1){
close(read_fd);
return false;
}
int result = sendfile(write_fd, read_fd, &offset, stat_buf.st_size);
printf("result=%d, err=%s\n", result,strerror(errno));
close(read_fd);
close(write_fd);
return result > 0;
}
if (read_fd = open(src_filename, O_RDONLY) == -1)
You left out some parentheses. The == operator has higher precedence than =. So assuming open() succeeds, you are setting read_fd to 0. Thus your sendfile is trying to read from standard input, which probably isn't a regular file. sendfile only supports reading from regular files. Hence failure.
Compiling with gcc -Wall will give you a warning about this: suggest parentheses around assignment used as truth value.
Always use compiler warnings, and never ignore them!