0

I have scanned "ABCDEFGHIJK" with fscanf into a char array[26]. Then I got "ABCDEFGHIJK" using "for" into another array[11]. Now I need to get to an "ABCDEFGHIJK.mp4" array[15] in order to feed that filename into a rename function.

I don't know much about C programming besides printf, scanf, for and while.

    sprintf(filename, "%c%c%c%c%c%c%c%c%c%c%c.mp4", codigo[0], codigo[1], codigo[2], codigo[3], codigo[4], codigo[5], codigo[6], codigo[7], codigo[8], codigo[9], codigo[10]);

This code above seems to work, but I wonder if there is a simpler way (especially for bigger arrays)?

Clarification: I have a txt file, formatted with these containing the filenames without extension and the human filename. I'm trying to make a program that will rename these files to the correct name, as they are from a backup that failed.

EDIT: Here is the full program. I renamed the variables to make more sense, so "codigo" is now "idcode".

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    int i, j, k;

    char value1[26], value2[70], idcode[11], filename[16], fullname[64], filenamestring[16], fullnamestring[64];

    // Opening file.

    FILE *nomes;
    nomes = fopen("/Users/EA/Desktop/Songs/backup.txt", "rt");
    if(nomes == NULL)
    {
        printf("Erro ao abrir backup.txt");
        exit(1);
    }

    // Skipping beggining of file until first value.

    for(i=0; i<10; i++)
        {
            fscanf(nomes, "%*s");
        }

    // Reading first value, and repetition.

    while(fscanf(nomes, "%s", value1) == 1)
    {
        j = k = 0;

        // Extracting idcode from value1.

        for(i=7; i<18; i++)
        {
            idcode[j] = value1[i];
            j++;
        }

        // Filling the complete filename.

        sprintf(filename, "%.*s.mp4", 11, idcode);

        // Reading second value, the "human" filename.

        fscanf(nomes, "\n%[^\n]", value2);
        for(i=6; i<70; i++)
        {
            fullname[k] = value2[i];
            k++;
        }

        // Transforming filenames into strings for rename function.

        strncpy(filenamestring, filename, 16);
        strncpy(fullnamestring, fullname, 64);

        // Renaming the files.

        rename(filename, fullname);

        // Skipping useless data before the next cycle.

        for(i=0; i<9; i++)
        {
            fscanf(nomes, "%*s");
        }
    }

    // Closing file and ending program.

    fclose(nomes);
    return(0);
}
  • Assuming that the arrays are arrays of `char`, why not simple put a string terminator last and use it as a string? – Some programmer dude Nov 09 '15 at 12:36
  • If, for example, the `array[15]` was actually one spot bigger, `array[16]`, then the `char array` could include the NULL terminator at the end, thus making the `char array` into your typical C string. – Chris O Nov 09 '15 at 12:37
  • No need for a it to be a zero-terminated string, just use `%.*s`. – Leandros Nov 09 '15 at 12:38

2 Answers2

0

It looks like you got an array of char's. You can simply do:

sprintf(filename, "%.*s.mp4", 11, codigo)

You can read more about the %.*s specifier in this question.

Community
  • 1
  • 1
Leandros
  • 16,805
  • 9
  • 69
  • 108
  • While it's a link to the C++ reference, it's still the C documentation. I removed it anyway, please remove your downvote. It's not necessary. I also removed your edit, it's potentially unsafe, if `codigo` wasn't allocated on the stack it'll just return the size of the pointer. – Leandros Nov 09 '15 at 13:02
  • My edit was because you use `snprintf()`!!! Your answer deserves the downvote. Size of which pointer? – Iharob Al Asimi Nov 09 '15 at 13:06
  • @iharob The `sprintf` is fine because an upper bound the length of the string substitution is statically known. – fuz Nov 09 '15 at 13:09
  • 1
    @FUZxxl I mean that the original answer had `snprintf(filename, "%.s.mp4", 15, codigo)`. – Iharob Al Asimi Nov 09 '15 at 13:16
  • That must've been a typo, sorry, you could've just removed the superflous n. – Leandros Nov 09 '15 at 13:23
  • What's the downvote for? The answer is perfectly valid. Explain or remove. – Leandros Nov 09 '15 at 13:23
  • @iharob I see, but now it's fine, isn't it? – fuz Nov 09 '15 at 13:37
  • 2
    ...almost fine: only 11 characters need to be printed from `codigo`, not 15 (including `.mp4` it is 15). – Paul Ogilvie Nov 09 '15 at 13:38
  • Thank-you! It worked. But now there is another problem. The files are not being renamed, so the program is not doing anything... –  Nov 09 '15 at 14:45
  • That is a different question. Open a new question. – Leandros Nov 09 '15 at 15:14
0

If you just want to append the format ".mp4" to the end of the string the easiest way to do that is just:

strcat(filename,".mp4");

after you printed the name without the ".mp4" into it. If you really want to use sprintf then i think this should suffice:

sprintf(filename,"%s.mp4", codigo);

Also,if you want your string to be bigger and not fixed in size you can put "\0" at the end of it(this might not work if you try to use the strings in other programming languages but c) with strcat as above,or you can do this:

memset(&filename, 0, sizeof(filename));