LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C character appending (https://www.linuxquestions.org/questions/programming-9/c-character-appending-269413/)

Chrax 12-22-2004 10:51 PM

C character appending
 
So I'm making several files, file.1, file.2, all the way up to file.n, and they're going to be created as I reach a certain point in a loop. For example, each time we run through the loop we're going to write a line to a file. When we get to the 100,000,000th line, we'll start on a new file.

I'm not particularly experienced with strings in C, but what I want to do is keep track of which number I'm on, and then create the file names as needed. Here's the source of what I've tried:

Code:

#include <stdio.h>
#include <gmp.h>

int main(int argc, char **argv)
{
  FILE *infile;
  FILE *outfile;
  mpz_t trans;
  mpz_init(trans);
  long long count = 0;
  int fileno = 49;

  infile = fopen("/asdf/primelog","r");
  outfile = fopen("/asdf/primelog.1","w");

  while(gmp_fscanf(infile,"%Zd",&trans) != EOF){
    if(count++ == 100000000){
      fileno++;
      fclose(outfile);
      char *filename = "/asdf/primelog." + fileno;
      outfile = fopen(filename,"w");
    }
    gmp_fprintf(outfile,"%Zd\n",trans);
  }
  fclose(outfile);
}

But C apparently doesn't like + concatenations. How would I go about adding a number on at the end? (For the moment, I'm assuming we're not going multidigit. That shouldn't be too much of a problem once I've got this bit.)

itsme86 12-22-2004 11:42 PM

I would do something like this:
Code:

itsme@dreams:~/C$ cat inc.c
#include <stdio.h>

int main(void)
{
  char filename[100];
  int i;

  for(i = 1;i <= 10;++i)
  {
    sprintf(filename, "/asdf/primelog.%d", i);
    printf("%s\n", filename);
  }

  return 0;
}

And then when you run it you can see what the filenames are in this example...
Code:

itsme@dreams:~/C$ ./inc
/asdf/primelog.1
/asdf/primelog.2
/asdf/primelog.3
/asdf/primelog.4
/asdf/primelog.5
/asdf/primelog.6
/asdf/primelog.7
/asdf/primelog.8
/asdf/primelog.9
/asdf/primelog.10

Anyway, I think sprintf() is what you're looking for.

Chrax 12-23-2004 12:42 AM

That's exactly what I needed. Thanks.

bm17 12-23-2004 03:08 AM

Just a general comment here: C/C++ is a low-level language. That's a good thing, in my opinion, because it is efficient. When you write code in C or C++ you are basically one step above the assembler. You are moving bits around on the machine. If you want a language that does-what-you-mean then you should use java or perl or something like that. I'm not saying that one language is better than the other, but if you are going to work in the C/C++ world, then you would be well served by an understanding of the way the compiler maps data types and instructions to the underlying hardware. C/C++ are not friendly languages, but they are extremely efficient.

Chrax 12-23-2004 03:34 AM

I wasn't looking for advice on which language to use, I was looking for a specific answer to a specific problem. Yes, it would be easier to do in a high-level language, but that doesn't mean I shouldn't learn how to use low-level languages. While your general comment is duly noted, it is was both irrelevant and condescending.

bm17 12-23-2004 08:32 AM

I apologize if the comment seemed condescending. I wasn't offering advice on which language to use; I was offering advice about about how to think about the machine instructions generated by your C source code. One of the biggest problems with C programs is buffer overruns because programmers are not thinking at the binary level.

Also, in your code you should probably be resetting "count" to zero whenever you open a new file. Otherwise you will only ever switch filenames once.

Chrax 12-23-2004 11:32 AM

Yeah, I already fixed that. Anyway, that was a small program to transfer my log to a new naming scheme without having to reproduce the logs. I maxed out the file size on a prime list. So today we're debugging the actual program.


All times are GMT -5. The time now is 09:07 AM.