Can someone outline the "best practice" (if any!) to handle newline in ANSI-C standard library functions?
I had some confusion with these functions recently related to char array and char pointer. puts(), printf(), strcpy(), strncpy(), memset().
I seem to understand their basic use, but got quite confused on some situations.
Code:
#include <stdio.h>
#include <string.h>
//Some said don't use strcpy() but use strncpy()!!!
int main () {
char str1[128];
char str2[256];
int len1, len2;
// strcpy(str1, "this is string1");
// strcpy(str2, "That is test string2");
len1=strlen("this is string1");
len2=strlen("That is test string2");
//memset(str1, '\0', sizeof(str1)); //why is it needed?
strncpy(str1, "this is string1", len1);
strncpy(str2, "That is test string2", len2);
puts("___Line 1___");
puts(str1);
puts("___Line 2___");
puts(str2);
return (0);
}
1) For unknown reason, sometime I got output:
Code:
this is string10That is test string2
That is test string2.
With the memset() function inserted, I always got the correct output. But, I do not see memset() is combined with these strncpy()/strcpy() very often.
2) The 4 puts() are in a row, but sometime they gave extra blank line that seems come from extra newline (especially in the next example), but I read puts() only appends newline at the end.
Here is another example where I tested with printf() and puts():
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//Tried from scratch combining const/strlen/malloc/'\0' concepts
void swap(char* s1, char* s2) {
*s1 = *s1 ^ *s2;
*s2 = *s2 ^ *s1;
*s1 = *s2 ^ *s1;
}
char* str_reverse_in_place_by_swap(char* str){
int len = strlen(str);
char* start = str;
char* end = start + len - 1; //-1 for '\0'; Made a mistake: char* end = start + len
;
while(start < end )
{
swap(start, end);
++start;
--end;
}
return str;
}
int main(int argc, char *argv[])
{
char* string1 = malloc(256*sizeof(char)); //first allocate 256 bytes long
printf("Type a String to reverse it[max. 255 chars]:\n");
if (fgets(string1, 256, stdin) == NULL)
printf("Error! Non-empty string is needed!\n");
char* str3 = malloc( (strlen(string1)+1) * sizeof(char) );
printf("Before reverse string1(which should be empty!): %s\n", str3);
printf("By reverse_in_place_by_swap():");
// str3 = str_reverse_in_place_by_swap(string1);
// puts(str_reverse_in_place_by_swap(string1));
printf("%s\n", str_reverse_in_place_by_swap(string1));
free(str3);
return EXIT_SUCCESS;
}
I must have missed important rules to use these functions.
so I'm asking my question with "best practice" to narrow down my struggle if that is reasonable. Thanks.
Dear All,
I have to develope some C functions in Unix for a Magic program. The original MSE code which compiles the attached C program uses a +z option, but the cc compiler don't know this. The complete command in the compiler script is 'cc -c -Aa +z myfile.c'. The warning message is 'The -z... (4 Replies)
can somebody explain about the ANSI C vs POSIX. say i was using open and fopen, i know that open is POSIX, and fopen is ANSI C. i read that that POSIX is a system call and ANSI C is like a standard library function. wouldn't the fopen function has to call on open function anyway to open any kind... (2 Replies)
Hi,
I am creating a file in Unix using a shell script. The file is getting created in the Unix - ANSI format. My requirement is to convert it to the PC - ANSI format. Can anyone tell me how to do this?
Thanks,
Sunil (0 Replies)
Is it not a healthy practice to mix C and C++ standard I/O functions together
e.g.
string name; // this is a declared instance of the string class in C++
printf("\nPlease enter your name: ");
cin >> name;
I did something similar in a program Im designing, and used it several... (1 Reply)
Hi everybody,
first of all i apologize if my thread's title doesn't make much sense,but i coudn't find a more appropriate name :)
Then i apologize about my question,which probably will sound trivial for you :) :)
I am working on a program which is being tested in Linux but the final target is... (2 Replies)
Hi there
how can i get the result of a command to not give me its error. For example, on certain systems the 'zfs' command below is not available, but this is fine becaues I am testing against $? so i dont want to see the message " command not found" Ive tried outputting to /dev/null 2>&1 to no... (5 Replies)
Hey, I'm completely new at this and I was wondering if there is a way that I would be able to redirect the log files in a directories standard output and standard error into and excel spreadsheet in anyway?
Please remember don't use too advanced of terminology as I just started using shell... (6 Replies)
I need to run a cronjob and in the cronjob I execute a script that if there is an error produces standard error so I do
/RUNMYSCRIPT 2> mylogfile.log
However, if it runs correctly, I don't get a standard error output, I get a standard out output. How do I redirect both standard error and... (2 Replies)
I follow the description of wiki (Lamport's bakery algorithm - Wikipedia, the free encyclopedia), then implement that algorithm in C, but it doesn't work, Starving is still here, is the implementation worry?
Only print out:
Thread ID: 0 START!
Thread ID: 0 END!
Thread ID: 0 START!... (2 Replies)
I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line
if
then... (8 Replies)