I wrote a simple C program in Linux, which used Itoa. But when compiling, I prompted “undefined reference to ` Itoa ‘”, I thought it would be OK to add – LC, but the result was the same. Internet found that some people say that this function does not exist in Linux, generally use sprintf to replace it. Look at the following code and comments:
#include <stdio.h>
#include <stdlib.h>
//#include <unistd.h>
#include <string.h>
int num = 0;
char namebuf[100];
char prefix[] = "/tmp/tmp/p";
char* gentemp()
{
int length, pid;
pid = getpid();
strcpy(namebuf, prefix);
length = strlen(namebuf);
//itoa(pid, &namebuf[length], 10); // Unix version: itoa() does not exist in header file <stdlib.h>
sprintf(namebuf+length, "%d", pid); // Converting integers to strings using sprintf
strcat(namebuf, ".");
length = strlen(namebuf);
printf("before do...while\n");
char command[1024] = {0};
do
{
//itoa(num++, &namebuf[length], 10);
sprintf(namebuf+length, "%d", num++);
sprintf(command, "touch %s", namebuf); // Creating files via touch
system(command);
printf("command = %s, namebuf[%d]=%d\n", command, num-1, num-1);
} while (num < 50 && access(namebuf, 0) != -1); // access to determine whether a file exists
printf("end of do...while\n");
return namebuf;
}
int main( void )
{
char *p = gentemp();
printf("%s\n", p);
return 0;
}
Read More:
- [Linux] undefined reference to `itoa’
- How to Fix Ubuntu(Linux) mount error(22)
- linux(aarch64): How to Fix kettle Startup Error Issue
- How to Fix Linux sub process /usr/bin/dpkg returned an error code (1)
- How to Fix Oracle listener error Linux error: 111: connection reused
- WPF: How to Reference Font Resource File
- How to fix the ERROR: Failed to build gem native extension in Centos 8
- [Realtek sdk-4.4.1] RTL8198D How to Fix pptp/l2tp HW checksum error issue
- How to Fix error 28 from storage engine
- How to Create Threads in Linux
- Grep: How to Find All the Files Containing a String in Linux
- Linux: How to Solve vim ~/.bashrc Execute Error
- How to Solve Linux Error: -bash: unzip: command not found
- Linux: How to Solve sudo operate Error
- How to Fix No default.conf file in conf.d after Installing Nginx
- Linux: How to Solve Rabbitmq Plug-in Install Error
- How to Solve Android Linux5.10 Kernel do_gettimeofday Function Error
- Linux: How to Solve pyperclip Module Error
- How to Solve No approve protocol error when running kettle on Linux
- [Solved] Linux — 9 — txt files are copied from windows to Linux and read error