C How to turn strings passed by command into array - c

I'm trying to turn strings into arrays. However, when I try to print the values to test it, it won't print anything.
Command
a.c BTC IOT NEO ETH XRP
Expected Output
BTC.csv
IOT.csv
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
typedef struct
{
char m[9];
}moeda;
int main(int argc, char *argv[]){
FILE *csv;
int n = argc-1;
moeda *m;
m = (moeda*)malloc(n*sizeof(moeda));
for(int z=1; z<=n; z++)
{
int i = 0;
sprintf(m[i].m, "%s.csv", argv[z]);
i++;
}
printf("%s\n", m[0].m);
printf("%s\n", m[1].m);
return 0;
}

You declare i inside the loop, meaning it gets set to 0 for each iteration. Try
int i = 0;
for(int z=1; z<=n; z++)
{
sprintf(m[i].m, "%s.csv", argv[z]);
i++;
}
Or just
for(int z=1; z<=n; z++)
{
sprintf(m[z - 1].m, "%s.csv", argv[z]);
}

Related

why do I get "Segmentation fault" when assigning values to array of pointers

I have this peace of C Programming code to take multiple literal strings from the user and store each address to each pointer and print out the value
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *ptr[3];
int i = 0;
for (; i < 3; i++) {
printf("ptr_%d: ", i + 1);
fgets(ptr[i], 15, stdin);
ptr[i][strlen(ptr[i]) - 1] = 0;
puts(ptr[i]);
}
return 0;
}
However, only the first one is printed. Here is the output
ptr_1: first line
first line
Segmentation fault
[Program finished]
I want the same result that is produced Here
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *ptr[] = {
"first line",
"second line",
"third line"
};
puts(ptr[0]);
puts(ptr[1]);
puts(ptr[2]);
return 0;
}
output
first line
second line
third line
[Program finished]
Thanks in advance
fgets(ptr[i], 15, stdin);
You've declared an array of three pointers:
char *ptr[3];
But none of those actually point to buffers of memory.
You can either create those buffers automatically:
char ptr[3][15];
Or dynamically with malloc.
char *ptr[3];
for (size_t i = 0; i < 3; i++) {
ptr[i] = malloc(15);
}
If you do this, make sure to free the memory you've allocated.
If you are running gcc (with glibc 2.7 or greater), you can use the m modifier with scanf to allocate memory:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *ptr[3];
for (i = 0; i < 3; i++) {
printf("ptr_%d: ", i + 1);
while (scanf(" %m[^\n]",&ptr[i]) != 1)
printf("Try again: ");
puts(ptr[i]);
}
for (; i < 3; i++)
free(ptr[i]);
return 0;
}
And be sure to free the memory when you are done with it.
You'd probably want to put the scanf section of this code into a function but here is the smallest change to your existing sample that should work.
$ cat allocinput.c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 80
int main(int argc, char *argv[])
{
char c;
char ptr[3][MAX_LEN];
int i = 0;
for (;i<3;i++) {
printf("ptr_%d: ", i + 1);
// could overflow if the user types more than MAX_LEN characters
char *p = ptr[i];
while (scanf("%c", &c) && (p - ptr[i] < MAX_LEN)) {
if (c == '\n') break;
*p++ = c;
*p = 0;
}
puts(ptr[i]);
}
return 0;}
$ gcc -Wall allocinput.c
$ ./a.out
ptr_1: first line
first line
ptr_2: second line
second line
ptr_3: third line
third line
$
P.S. I recommend astyle to clean up the formatting:
$ astyle allocinput.c
Formatted /tmp/overflow/allocinput.c
$ cat allocinput.c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 80
int main(int argc, char *argv[])
{
char c;
char ptr[3][MAX_LEN];
int i = 0;
for (; i<3; i++) {
printf("ptr_%d: ", i + 1);
// could overflow if the user types more than MAX_LEN characters
char *p = ptr[i];
while (scanf("%c", &c) && (p - ptr[i] < MAX_LEN)) {
if (c == '\n') break;
*p++ = c;
*p = 0;
}
puts(ptr[i]);
}
return 0;
}

Program doesn't work after it loops the second time

My program below is trying to add the usernames from the "Usernames.text" to the API url. The first username works and makes the url "https://api.1234ng.com/users/profiles/planeraft/Username1" but once it loops back it makes the url always "https://api.1234ng.com/users/profiles/planeraft/" instead of adding the next username in the file.
My code:
#include <stdio.h>
#include <string.h>
int Counter;
int UsernameSize;
char Username[255];
char Request[255]="https://api.1234ng.com/users/profiles/planeraft/";
FILE *UsernamesFile;
int main() {
UsernamesFile = fopen("Usernames.text", "r");
while (Counter < 100) {
fgets(Username, 255, UsernamesFile);
UsernameSize = strlen(Username);
for (int z; z < UsernameSize; z++) {
Request[48 + z] = Username[z];
}
printf("%s", "\n");
printf(Request);
for (int a; a < UsernameSize; a++) {
Request[48 + a] = '\0';
}
Counter++;
}
fclose(UsernamesFile);
return 0;
}
Variables a and z in the for loops are not initialized and thanks to DiKetarogg for some improvements to the code.
Several errors fixed below, more improvements are possible, see comments above and in code below:
#include <stdio.h>
#include <string.h>
int Counter = 0; // need to init
int UsernameSize;
char Username[255];
char Request[255]="https://api.test.com/users/profiles/test/";
// 01234567890123456789012345678901234567890
// 1 2 3 4
FILE *UsernamesFile;
int main() {
UsernamesFile = fopen("Usernames.text","r");
while(Counter < 3) {
fgets(Username, 255, UsernamesFile);
strcat(Request, Username);
UsernameSize = strlen(Username);
Username[UsernameSize-1] = '\0'; // remove <cr>
for(int z=0; z < UsernameSize; z++) {
Request[41+z]=Username[z];
}
printf("%s\n", Request);
Counter++;
strcpy(Request, "https://api.test.com/users/profiles/test/");
}
fclose(UsernamesFile);
return 0;
}

Print inputs in back wards. take Input from Command Line and its not printing backward

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
int i=0;
for (i=1; i<argc;i++){
Counting string length.
int l = strlen(argv[i]);
int a=1;
int start = 0;
int end = l-1;
I think i have problem in looping.
for (a=start;a>=end;a--)
printf ("%c",argv[a][i]);
printf("\n");
}
return 0;
}
I think you want this:
for (a=end;a>=start;a--)
printf ("%c",argv[i][a]);
Your code had start and end swapped and also a and i.
Example answer below:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
for (i = argc-1; i >0; i--)
printf("%s%s", argv[i], (i < argc-1) ? " " : "");
return 0;
}
// there is no need for extra variables - just one but the for loop starts from max argc and end ends when zero is reached (nearly)

Getting Segmentation Fault on simple loops

I'm trying to execute this code (yes, with that two lines commented out), but every time I get a Segmentation Fault.
I can't understand why.
(linux, gcc)
#include <stdio.h>
#include <math.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *current;
while(strcmp("99999999zz", current) != 0)
{
for(int i = 0; i < pow(10, 10); i++)
{
sprintf(current, "%010d", i);
printf("%s\n", current);
for(int a = 97; a <= 122; a++)
{
for(int j = 0; j < 10; j++)
{
//current[j] = (char)a;
//printf("%s\n", current);
}
}
}
}
}
This code, instead, runs without problems:
#include <stdio.h>
#include <math.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *current;
while(strcmp("99999999zz", current) != 0)
{
for(int i = 0; i < pow(10, 10); i++)
{
sprintf(current, "%010d", i);
printf("%s\n", current);
}
}
}
You invoked undefined behavior in both programs by using value of uninitialized variable having automatic storage duration, which is indeterminate.
You should declare an array instead of a pointer and initialize it.
#include <stdio.h>
#include <math.h>
#include <string.h>
int main(int argc, char *argv[])
{
double limit = pow(10, 10); /* calculating this every time in the loop may cause loss of performance */
char current[128] = ""; /* allocate enough memory and initialize */
while(strcmp("99999999zz", current) != 0)
{
for(int i = 0; i < limit; i++)
{
sprintf(current, "%010d", i);
printf("%s\n", current);
for(int a = 97; a <= 122; a++)
{
for(int j = 0; j < 10; j++)
{
//current[j] = (char)a;
//printf("%s\n", current);
}
}
}
}
}

C Programming Command Line Argument

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int args, char *argv[]) {
int i = 0;
for (i = 0; i < args; i++)
printf("\n%s", argv[i]);
return 0;
}
As of now this program prints out whatever is written on the command line. How would I make it do this in reverse? For example, if I input "dog" it should say "god".
Try the following code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
int i = 0;
for (i = 1; i < argc; i++)
{
char *tmp = argv[i];
int len = strlen(argv[i]);
for(int j = len-1; j > -1; --j)
printf("%c",tmp[j]);
printf("\n");
}
return 0;
}
I'd break this down into two smaller tasks.
Write a helper function that, given a string, prints it out in reverse order. To do so, you could use a loop that starts at the end of the string and prints each character one after the other, moving in reverse across the array as you go.
Call that helper function in main inside the loop to print each string in the argv array.
Since this looks like a homework assignment, I'll leave it at this. If you're having trouble with step (1), then you may need to review how to do string processing a bit before jumping into this problem.
IDEOne Link
int main(int argc, char *argv[]) {
for (int i = 0; i < argc; ++i)
{
for(char* c = &argv[i][strlen(argv[i])-1]; c >= argv[i]; putchar(*c--)) ;
putchar(' ');
}
return 0;
}

Resources