Search Results

Search found 916 results on 37 pages for 'stdio'.

Page 13/37 | < Previous Page | 9 10 11 12 13 14 15 16 17 18 19 20  | Next Page >

  • Segmentation fault when using files C++

    - by Popa Mihai
    I am using ubuntu 12.04. I have been trying a few IDE's for simple C++ school projects. However, with codelite, anjuta and kdevelop I encountered a problem: when I am trying to read / write in files I get segmentation fault: core dumped. I am using a basic source: #include<stdio.h> FILE*f=fopen("test.in","r"); FILE*g=fopen("test.out","w"); int main () { int a,b; fscanf(f,"%d %d",&a,&b); fprintf(g,"%d\n",a+b); fclose(f); fclose(g); return 0; } I have to say that programs with stdin/stdout work well. Thank you,

    Read the article

  • Memory error, access violation.

    - by Ordo
    Hello! I'm learning C on my own and as a exercise i have written a program but it does not work. The program is splitted into 3 parts. A header file, a main file for executing the program a file to define the functions. I'm not using all the functions yet but that shouldn't be the problem. Here is my header file, nothing special in it. #ifndef EMPLOYEE_H #define EMPLOYEE_H struct Employee { char first[21]; char last[21]; char title[21]; int salary; }; struct Employee* createEmployee(char*, char*, char*, int); // Creates a struct Employee object on the heap. char* getfirstname (struct Employee*); char* getlastname (struct Employee*); char* gettitle (struct Employee*); int getsalary (struct Employee*); void setfirstname (struct Employee*, char*); void setlastname (struct Employee*, char*); void settitle (struct Employee*, char*); void setsalary (struct Employee*, int); void printEmployee(struct Employee*); #endif In this file i define the functions and how they work: #include "7.1.h" #include <stdio.h> #include <stdlib.h> #include <string.h> struct Employee* createEmployee(char* first, char* last, char* title, int salary) // Creates a struct Employee object on the heap. { struct Employee* p = (struct Employee*) malloc(sizeof(struct Employee)); if (p != NULL) { strcpy(p->first, first); strcpy(p->last, last); strcpy(p->title, title); p->salary, salary; } return p; } char* getfirstname (struct Employee* p) { if (p != NULL) return p ? p->first : ""; } char* getlastname (struct Employee* p) { if (p != NULL) return p ? p->last : ""; } char* gettitle (struct Employee* p) { if (p != NULL) return p ? p->title : ""; } int getsalary (struct Employee* p) { if (p != NULL) return p ? p->salary : 0; } void setfirstname (struct Employee* p, char* first) { if (p != NULL) strcpy(p->first, first); } void setlastname (struct Employee* p, char* last) { if (p != NULL) strcpy(p->last, last); } void settitle (struct Employee* p, char* title) { if (p != NULL) strcpy(p->title, title); } void setsalary (struct Employee* p, char* salary) { if (p != NULL) p->salary, salary; } void printEmployee(struct Employee* p) { if (p != NULL) { printf("%s, %s, %s, %d", p->first, p->last, p->salary, p->salary ); } } And the last file is used to executed the program/functions: #include "7.1.h" #include <stdio.h> #include <stdlib.h> int main () { char decision; struct Employee emp; struct Employee* emps[3]; for ( int i = 0; i < 1; i ++) { printf("Please type in the emplooyes data.\nFirstname:"); scanf("%s", emp.first); printf("Lastname:"); scanf("%s", emp.last); printf("Title:"); scanf("%s", emp.title); printf("Salary:"); scanf("%d", &emp.salary); emps[i] = createEmployee(emp.first, emp.last, emp.title, emp.salary); } printf("Do you want to print out your information? (Y/N):"); scanf("%c", &decision); if (decision == 'y' || decision == 'Y') { printEmployee(emps[1]); } } I don't know what the problem is. I 'm always getting the following error message after typing in first, last, title and salary for the first time. The error is written in german. It means: Unhandled exception at 0x102de42e (msvcr100d.dll) in 7.1.exe: 0xC0000005: Access violation when writing to 0xCCCCCCCC position. I could fix the first problem with the hints given below. Now when i want to print out the employee data using the function:printEmployee(emps[1]);, I get the same kind of error with access violation.

    Read the article

  • casting issue with realpath function (c programming)

    - by Ralph
    When I compile the following code: #define _POSIX_C_SOURCE 200112L #define _ISOC99_SOURCE #define __EXTENSIONS__ #include <stdio.h> #include <limits.h> #include <stdlib.h> int main(int argc, char *argv[]) { char *symlinkpath = argv[1]; char actualpath [PATH_MAX]; char *ptr; ptr = realpath(symlinkpath, actualpath); printf("%s\n", ptr); } I get a warning on the line that contains the call to the realpath function, saying: warning: assignment makes pointer from integer without a cast Anybody know what's up? I'm running Ubuntu Linux 9.04

    Read the article

  • Sockets: Transport endpoint is not connected on send

    - by TheoretiCAL
    I'm trying to learn socket programming from http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html and am attempting to build a SOCK_STREAM client/server. My client: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #define SERVERPORT "4951" // the port users will be connecting to int main(int argc, char *argv[]) { int sockfd; struct addrinfo hints, *servinfo, *p; int rv; int numbytes; if (argc != 3) { fprintf(stderr,"usage: talker hostname message\n"); exit(1); } memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if ((rv = getaddrinfo(argv[1], SERVERPORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } // loop through all the results and make a socket for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("talker: socket"); continue; if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("client: connect"); continue; } } break; } if (p == NULL) { fprintf(stderr, "talker: failed to bind socket\n"); return 2; } if ((numbytes = send(sockfd, argv[2], strlen(argv[2]), 0) == -1)) { perror("talker: send"); exit(1); } freeaddrinfo(servinfo); printf("talker: sent %d bytes to %s\n", numbytes, argv[1]); close(sockfd); return 0; } Server: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #define MYPORT "4951" // the port users will be connecting to #define MAXBUFLEN 100 static int backlog = 10; // get sockaddr, IPv4 or IPv6: void *get_in_addr(struct sockaddr *sa) { if (sa->sa_family == AF_INET) { return &(((struct sockaddr_in*)sa)->sin_addr); } return &(((struct sockaddr_in6*)sa)->sin6_addr); } int main(void) { int sockfd; struct addrinfo hints, *servinfo, *p; int rv; int numbytes; int new_fd; socklen_t addr_size; struct sockaddr_storage their_addr; char buf[MAXBUFLEN]; char s[INET6_ADDRSTRLEN]; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4 hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // use my IP if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } // loop through all the results and bind to the first we can for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("listener: socket"); continue; } int yes=1; // lose the pesky "Address already in use" error message if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { perror("setsockopt"); exit(1); } if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("listener: bind"); continue; } if (listen(sockfd,backlog) == -1){ close(sockfd); perror("listener:listen"); continue; } break; } if (p == NULL) { fprintf(stderr, "listener: failed to bind socket\n"); return 2; } freeaddrinfo(servinfo); printf("listener: waiting to recv..\n"); while(1){ addr_size = sizeof their_addr; if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size))==-1){ perror("accept"); exit(1); } if ((numbytes = recv(new_fd, buf, MAXBUFLEN-1 , 0) == -1)) { perror("recv"); exit(1); } printf("listener: got packet from %s\n", inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s)); printf("listener: packet is %d bytes long\n", numbytes); buf[numbytes] = '\0'; printf("listener: packet contains \"%s\"\n", buf); close(sockfd); } return 0; } Upon executing the client, I get " send: Transport endpoint is not connected" and I'm not sure where I went wrong. Thanks.

    Read the article

  • this == null; surely not, but yes

    - by pm100
    I would have said that this will segfault but apparently not #include <stdio.h> class foo { public: bool thingy() {return this == 0;}; }; int main() { foo *f = 0; if(f->thingy()) printf("eating hat\n"); } I just expect that 0-xxx will never work. I assume that if we had to do some vtabling it would fail; obviously cannot access any member variables either edit; sorry - no question there. THe question is "are you surprised like me?" and it seems the answer is yes. even though nobody said yes. I got a) of course this will work b) of course this will never work c) it is undefined behavior; your mileage will vary

    Read the article

  • Function argument treated as undeclared

    - by Mikulas Dite
    I've prepared this simple example which is not working for me #include <stdio.h> #include <stdlib.h> FILE *fp; char filename[] = "damy.txt"; void echo (char[] text) { fp = fopen(filename, "a"); fwrite(text, 1, strlen(text), fp); fclose(fp); printf(text); } int main () { echo("foo bar"); return 0; } It's supposed to write both to command window and to file. However, this gives compilation error - the text used in echo() is not declared. Does c need another declaration of the variable?

    Read the article

  • Complete state of a process

    - by sravan
    hi all, I wrote a small program which is as follows: #include<stdio.h> int c=0; int main() { int a=10,b=20; printf("Hello World\n"); c = a+b; printf("%d\n",c); return 0; } I can create a.out file using the command gcc -save-temps helloworld.c. The save-temps flag allows us to save the intermediate files, helloworld.i, helloworld.s, helloworld.o Now i want to know exactly how the stack of this program changes during the execution. Can some one please tell me how to go about it. My aim of this question is to know exactly what all happens during the execution of any program.

    Read the article

  • C pointer initialization and dereferencing, what's wrong here?

    - by randombits
    This should be super simple, but I'm not sure why the compiler is complaining here. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int *n = 5; printf ("n: %d", *n); exit(0); } Getting the following complaints: foo.c: In function ‘main’: foo.c:6: warning: initialization makes pointer from integer without a cast I just want to print the value that the pointer n references. I'm dereferencing it in the printf() statement and I get a segmentation fault. Compiling this with gcc -o foo foo.c.

    Read the article

  • C input loop for shell

    - by AustinM
    So I'm working on creating a very simple C program that just preforms shell commands. This is what I have so far: #include <stdio.h> int main() { char input[30]; fputs("$ ", stdout); fflush(stdout); fgets(input, sizeof input, stdin); system(input); } It works, but only for one command. For example if I compile and type ./cmd I get the $ prompt. If I type ls I get what I'm supposed to get. But then it exits and goes back to the regular system shell. How can I make it so after the user types a command it goes back to the "$" input.

    Read the article

  • How does C free() work?

    - by slee
    #include <stdio.h> #include <stdlib.h> int * alloc() { int *p = (int *)calloc(5,4); printf("%d\n",p); return p; } int main() { int *p = alloc(); free(p); printf("%d\n",p); p[0] = 1; p[1] = 2; printf("%d %d\n",p[0],p[1]); } As to the code segment, I allocate 5 ints,first. And then I free the memory. When I printf p, why does p sill have a value same to the memory address allocated first? And I also can assign value to p[0] and p[1]. Does this mean free() do nothing? Once I allocate memory, I can use later though I have freed it.

    Read the article

  • Why this C program outputs a negative number?

    - by JMSA
    I have assigned the complement value in an unsigned variable. Then why this C program outputs a negative number? #include<stdio.h> #include<conio.h> int main() { unsigned int Value = 4; /* 4 = 0000 0000 0000 0100 */ unsigned int result = 0; result = ~ Value; /* -5 = 1111 1111 1111 1011 */ printf("result = %d", result); /* -5 */ getch(); return 0; }

    Read the article

  • About using assembly with c

    - by kristus
    Hi. I've sort of just finished a mandatory task at school, and I'm about to deliver it. But then I came across something that was unfamiliar, header files. :( What I've got: test-program.c task_header.h function1.s function2.s function3.s function4.s test-program.c: #include <stdio.h> #include <stdlib.h> #include <string.h> #include "task_header.h" . .. ... task_header.h: extern void function1(...); extern void function2(...); extern int function3(...); extern void function4(...); And then I use the command: gcc -m32 -o runtest test-program.c function1.s function2.s function3.s function4.s Is this a proper way to do it, or is it possible to modify it? So I can type: gcc -m32 -o runtest test-program.c ?

    Read the article

  • Why do i get segfault at the end of the application after everything's beed done properly ?

    - by VaioIsBorn
    #include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char pass[] = "m4ak47"; printf("Vnesi password: \t"); scanf("%s", stole); if(strncmp(stole, pass, sizeof(pass)) != 0) { printf("wrong password!\n"); exit(0); } else printf("Password correct\n"); printf("some stuf here...\n\n"); return 0; } This program is working nice, but with one problem - if the password is correct then it DOES do the printing of 'some stuf here...' but it also shows me segmentation fault error at the end. Why ?

    Read the article

  • Same memory space being allocated again & again while using malloc()

    - by shadyabhi
    In each loop iteration, variable j is declared again and again. Then why is its address remaining same? Shouldn't it be given some random address each time? Is this compiler dependent? #include<stdio.h> #include<malloc.h> int main() { int i=3; while (i--) { int j; printf("%p\n", &j); } return 0; } Testrun:- shadyabhi@shadyabhi-desktop:~/c$ gcc test.c shadyabhi@shadyabhi-desktop:~/c$ ./a.out 0x7fffc0b8e138 0x7fffc0b8e138 0x7fffc0b8e138 shadyabhi@shadyabhi-desktop:~/c$

    Read the article

  • collect string in loop and printout all the string outside loop

    - by user1508163
    I'm newbie here and there is some question that I want have some lesson from you guys. For example: #include <stdio.h> #include<stdlib.h> #include<ctype.h> void main() { char name[51],selection; do { printf("Enter name: "); fflush(stdin); gets(name); printf("Enter another name?(Y/N)"); scanf("%c",&selection); selection=toupper(selection); }while (selection=='Y'); //I want to printout the entered name here but dunno the coding printf("END\n"); system("pause"); } As I know when the loops perform will overwrite the variable then how I perform a coding that will printout all the name user entered? I have already ask my tutor and he is ask me to use pointer, can anyone guide me in this case?

    Read the article

  • Convert Decimal number into Fraction

    - by alankrita
    I am trying to convert decimal number into its fraction. Decimal numbers will be having a maximum 4 digits after the decimal place. example:- 12.34 = 1234/100 12.3456 = 123456/10000 my code :- #include <stdio.h> int main(void) { double a=12.34; int c=10000; double b=(a-floor(a))*c; int d=(int)floor(a)*c+(int)b; while(1) { if(d%10==0) { d=d/10; c=c/10; } else break; } printf("%d/%d",d,c); return 0; } but I am not getting correct output, Decimal numbers will be of double precision only.Please guide me what I should do.

    Read the article

  • dreferencing 2 d array

    - by ashish-sangwan
    Please look at this peice of code :- #include<stdio.h> int main() { int arr[2][2]={1,2,3,4}; printf("%d %u %u",**arr,*arr,arr); return 0; } When i compiled and executed this program i got same value for arr and *arr which is the starting address of the 2 d array. For example:- 1 3214506 3214506 My question is why does dereferencing arr ( *arr ) does not print the value stored at the address contained in arr ?

    Read the article

  • How to give 'Everybody' full rights to a file (programmatically)

    - by egrunin
    I'm modifying an old C++ program to run on Vista. It does not require Admin privileges. I've changed the code to put logfiles in \ProgramData\MyApp\. These logfiles are written with the stdio functions (fopen, fprintf, fclose). Here's the problem: UserA runs the program first, it creates \ProgramData\MyApp\MyLogFile.txt UserB runs the program next, it tries to append to MyLogFile.txt and gets access denied. I tried creating a null SECURITY_DESCRIPTOR and passing that to CreateFile(). That does create a file with "no permissions assigned", but it seems as if the first user to write to the file takes ownership and afterwards all the other non-admin users are out of luck. It's important that all users share the same logfiles, but it's also important that I change as little code as possible.

    Read the article

  • how to make this piece of code work in C++?

    - by cambr
    #include<stdio.h> void print(int *arr[], int s1, int s2) { int i, j; printf("\n"); for(i = 0; i<s1; i++) { for(j = 0; j<s2; j++) { printf("%d, ", *((arr+i)+j)); } } printf("\n"); } int main() { int a[4][4] = {{0}}; print(a,4,4); } This works in C, but not in C++. error: cannot convert `int (*)[4]' to `int**' for argument `1' to `void print(int**, int, int)' Why does it not work in C++? What change is needed to be made?

    Read the article

  • Same memory space being allocated again & again

    - by shadyabhi
    In each loop iteration, variable j is declared again and again. Then why is its address remaining same? Shouldn't it be given some random address each time? Is this compiler dependent? #include<stdio.h> #include<malloc.h> int main() { int i=3; while (i--) { int j; printf("%p\n", &j); } return 0; } Testrun:- shadyabhi@shadyabhi-desktop:~/c$ gcc test.c shadyabhi@shadyabhi-desktop:~/c$ ./a.out 0x7fffc0b8e138 0x7fffc0b8e138 0x7fffc0b8e138 shadyabhi@shadyabhi-desktop:~/c$

    Read the article

  • How could my code compliled correctly without necessary headers?

    - by ZhengZhiren
    I use the functions fork(),exec()... But how can this program compiled without including some extra headers(like sys/types.h, sys/wait.h). I use ubuntu 10.04 with gcc version 4.4.3 #include <stdio.h> #include <stdlib.h> int main() { pid_t pid; printf("before fork\n"); pid = fork(); if(pid == 0) { /*child*/ if(execvp("./cpuid", NULL)) { printf("error\n"); exit(0); } } else { if(wait(NULL) != -1) { printf("ok\n"); } } return 0; }

    Read the article

  • Why does this program segfault

    - by Leda
    Upon compiling and running this small program to reverse a string, I get a Segmentation Fault before any output occurs. Forgive me if this is an obvious question, I'm still very new to C. #include <stdio.h> int reverse(char string[], int length); int main() { char string[] = "reversed"; printf("String at start of main = %s", string); reverse(string, sizeof(string)); printf("%s\n", string); return 0; } // Reverse string int reverse(char string[], int length) { int i; char reversed[] = {}; int temp; for(i = 0; i < length; ++i) { temp = string[i]; reversed[length - i] = temp; } return 0; }

    Read the article

  • C: stdin and std* errs

    - by user355926
    I want to my manipulate Stdin, then Std* but some errs: $ gcc testFd.c testFd.c:9: error: initializer element is not constant testFd.c:9: warning: data definition has no type or storage class testFd.c:10: error: redefinition of `fd' testFd.c:9: error: `fd' previously defined here testFd.c:10: error: `mode' undeclared here (not in a function) testFd.c:10: error: initializer element is not constant testFd.c:10: warning: data definition has no type or storage class testFd.c:12: error: syntax error before string constant $ cat testFd.c #include <stdio.h> #include <sys/ioctl.h> int STDIN_FILENO = 1; // I want to access typed // Shell commands, dunno about the value: unsigned long F_DUPFD; fd = fcntl(STDIN_FILENO, F_DUPFD, 0); fd = open("/dev/fd/0", mode); printf("STDIN = %s", fd);

    Read the article

  • Incompatible format types

    - by nebffa
    I'm playing around with strncpy in C and am having some trouble. The code is as follows: #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { char src[] = "Benjamin Franklin"; char dest[5]; strncpy(src, dest, sizeof(dest) / sizeof(char)); dest[5] = '\0'; printf("%s\n", dest); return 0; } which compiles with no errors using: gcc -Wall -g -Werror test.c -o test and prints out gibberish like p4?? I cannot really understand what I'm doing wrong especially since I have played around with it a lot and been looking online for answers. Perhaps since I am using arrays I am passing the address to printf without realising it?

    Read the article

< Previous Page | 9 10 11 12 13 14 15 16 17 18 19 20  | Next Page >