strcat in C


 
Thread Tools Search this Thread
Top Forums Programming strcat in C
# 1  
Old 07-15-2012
strcat in C

Hello,

Code:
#include <stdio.h>
#include <string.h>
void main()
{
char tab[2]={"12"};
FILE *outfile;
char *outname = "/home/dir/";
printf("%s",strcat(outname,tab));
outfile = fopen(strcat(outname,tab), "w");
 if (!outfile) {
  printf("There was a problem opening %s for writing\n", outname);
 }
 else {
 
 }
}

i have this error: Erreur de segmentation

Have you an idea please ?
Thank you.

Last edited by Scott; 07-15-2012 at 10:43 AM.. Reason: Code tags
# 2  
Old 07-15-2012
Code:
#include <stdio.h>
#include <string.h>
void main()
{
char tab[3]={"12"};
FILE *outfile;
char *outname = "/home/dir/";
printf("%s",strcat(outname,tab));
outfile = fopen(strcat(outname,tab), "w");
 if (!outfile) {
  printf("There was a problem opening %s for writing\n", outname);
 }
 else {
 
 }
}

There was no trailing NUL (the third character (red))
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 07-16-2012
There is also a problem with how you use strcat. This function "appends" the second argument (tab) to the first (outname) modifying outname. But since outname is a pointer to a const string, there is no room to do the concatenation. You also call strcat twice, so tab is appended twice.

This should work:

Code:
#include <stdio.h>
#include <string.h>
void main()
{
char tab[3]={"12"};
FILE *outfile;
char outname[80] = "/home/dir/";
strcat(outname,tab);
printf("%s\n",outname);
outfile = fopen(outname, "w");
 if (!outfile) {
  printf("There was a problem opening %s for writing\n", outname);
 }
 else {

 }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Programming

strcat outputs garbage

Anyone have any ideas why when using strcat function I would get some garbage at the beginning of the output string? what I'm doing is something like the following example. Code: char temp; char tempHolder; for(int i=0;i<something;i++){ sprintf(temp,"%u ", someVariable);... (2 Replies)
Discussion started by: airon23bball
2 Replies

2. Shell Programming and Scripting

strcat equivalent in shell scripting

Hi all, How does string concatenation work in shell scripting? I basically have a variable called "string" and I want to add the strings "aaa" "bbb" "ccc" "ddd" to the variable "string". These strings would be added based on some conditions and separated by spaces . So "string" might look... (8 Replies)
Discussion started by: felixmat1
8 Replies

3. Programming

`strcat' makes pointer from integer without a cast

A question to ask. seq1 = "eeeeeeeeeeeeeeeeee"; seq2 = "dddddddddddddddddddd"; char a = '*'; strcat(*seq2, &a); strcat(*seq1, seq2); compilation warning: passing arg 1 of `strcat' makes pointer from integer without a cast thanks (4 Replies)
Discussion started by: cdbug
4 Replies

4. Programming

strcat() dumping core

strcat dumping core in the situation like main() { char* item; char* p=sat_item; char type; item=(char*) malloc(strlen(p)); strncpy(type,p,4); type='\0'; strcat(item,type); //dumping core } I couldn't get why strcat dumping core? (3 Replies)
Discussion started by: satish@123
3 Replies
Login or Register to Ask a Question