LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-22-2004, 10:51 PM   #1
Chrax
Member
 
Registered: Apr 2004
Distribution: Dapper
Posts: 167

Rep: Reputation: 31
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.)
 
Old 12-22-2004, 11:42 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 12-23-2004, 12:42 AM   #3
Chrax
Member
 
Registered: Apr 2004
Distribution: Dapper
Posts: 167

Original Poster
Rep: Reputation: 31
That's exactly what I needed. Thanks.
 
Old 12-23-2004, 03:08 AM   #4
bm17
Member
 
Registered: Sep 2004
Location: Santa Cruz, CA, USA
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
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.
 
Old 12-23-2004, 03:34 AM   #5
Chrax
Member
 
Registered: Apr 2004
Distribution: Dapper
Posts: 167

Original Poster
Rep: Reputation: 31
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.
 
Old 12-23-2004, 08:32 AM   #6
bm17
Member
 
Registered: Sep 2004
Location: Santa Cruz, CA, USA
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
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.
 
Old 12-23-2004, 11:32 AM   #7
Chrax
Member
 
Registered: Apr 2004
Distribution: Dapper
Posts: 167

Original Poster
Rep: Reputation: 31
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems appending avis Blue1K Linux - General 3 06-27-2009 12:32 PM
appending to perllocal.pod via doinst.sh jong357 Slackware 1 10-31-2005 10:48 AM
return value from fopen when appending? linfan Programming 3 01-21-2004 01:21 PM
Appending a string to a file JStew Linux - General 13 03-04-2003 03:48 PM
[c] Appending to files using fopen() in mandrake wedamija Programming 4 11-01-2002 09:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration