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. 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 the header file <stdlib.h>
sprintf(namebuf+length, "%d", pid); // use sprintf to convert integers to strings
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: How to Fix undefined reference to `itoa’
- [Solved] Sed Failed to Reference Variable error: extra characters after command
- WPF: How to Reference Font Resource File
- [Solved] sed -i error: sed: -e expression #1, char 44: invalid reference \1 on `s’ command’s RHS
- [Solved] Syntax Error: TypeError: Cannot read property ‘parseComponent‘ of undefined (vue报错)
- [Solved] Cmake: symbol lookup error: cmake: undefined symbol: archive_write_add_filter_zstd
- [Solved] curl: symbol lookup error: curl: undefined symbol: curl_mime_free
- Linux Error: Failed to connect to ::1: No route to the host
- Ubuntu Startup Error: warning failed to connect to lvmetad,falling back to device scanning
- C#: How to Use Itextsharp to Manipulate PDF Files
- How to fix the ERROR: Failed to build gem native extension in Centos 8
- How to Solve Error: “Hdaudio hdaudioc0d2: unable to bind codec”
- [Solved] Win 10 VS Code Connect to the container of the server error: Cannot connect to the Docker daemon at … Is the docker daemon running
- How to Solve C++ Error: “no matching function for call to ‘atoi/atof’”
- Linux Nagios failed to log in to internal server error (Fixed)
- How to Solve Ubuntu “Failed to fetch” Error
- How to Export All data in DataGridView to Local Excel
- Android converts a view to bitmap + to handle the black edge of the view with rounded corners
- [Solved] Failed to Setup IP tables: Unable to enable SKIP DNAT rule: (iptables failed: iptables –wait -t na…
- ERROR: Failed to Setup IP tables: Unable to enable SKIP DNAT rule