creating multiple files

This is a discussion on creating multiple files within the C Programming forums, part of the General Programming Boards category; If I had an array of structures (like a database say) and I entered in 5 people. How could I ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    32

    creating multiple files

    If I had an array of structures (like a database say) and I entered in 5 people. How could I make a program that prints out a file for each of the people without actually coding the filename into the program.

    For example there's a guy named Joe in my database. I want to print out all of the info in his struct file (name and age for example). What I'm trying to do is make a program that runs through the array and for each person make a file called name.txt which contains a name and an age. So for Joe's file, it would be joe.txt. But I don't know how to do this without coding file=fopen("joe.txt", "w") into the program.

    This is a bit verbose and I don't know if this is very clear but I've tried to be. So yes, any help would be greatly appreciated. Thanks.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    create a vraible like fname that you can copy the person's name into, concat ".txt" then use
    file=fopen(fname, "w")
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    46
    I would do what Walt says. Just incase you need more direction:

    I didn't run the code below but you'll need lines like it.

    You have to make sure that the fname character array is plently long enough to hold the longest first name and the .txt after it. You can just make it way too long
    Code:
    struct person databasename[30];
    char fname[500];
    FILE *fp;
    
    //copy the 11th persons name
    strcpy(fname,databasename[10].name);
    
    //add the file extension
    strcat(fname,".txt");
    
    fp=fopen(filename,"w");
    
    //error checking to see that file did open

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    32

    It still isn't really working please have a look

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct person
    {
    	char name[20];
    	int receiver;
    
    };
    
    int main(void)
    {
    	struct person databasename[3];
    	char *fname;
    	FILE *fp;
    	int i;
    
    	for(i=0;i<3;i++)
    	{
    		printf("Enter name of player #%d:", i+1);
    		scanf("%s", databasename[i].name);
    	}
    
    	for(i=0;i<3;i++)
    	{
    		strcpy(fname,databasename[1].name);
    		strcat(fname,".txt");
    		printf("%s\n", databasename[1].name);
    		fp=fopen(fname,"w");
    		fprintf(fp,"TEST");
    		fclose(fp);
    	}
    	return 0;
    }
    output should be 3 text files that say "TEST"

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    32

    ignore "int receiver"

    Ignore the "int receiver" in the struct code. I just copied it from my actual program. It doesn't really have anything to do with this example program (which I can't seem to get working anyway).

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > char *fname;
    Is not pointing to any allocated memory

    char fname[25];
    would be better
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161

    Re: It still isn't really working please have a look

    Originally posted by lime
    output should be 3 text files that say "TEST"
    And what does it do. Doncha think that's an important piece of information to pass on?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006

    Re: It still isn't really working please have a look

    Code:
    	for(i=0;i<3;i++)
    	{
    		strcpy(fname,databasename[1].name);
    		strcat(fname,".txt");
    		printf("%s\n", databasename[1].name);
    		fp=fopen(fname,"w");
    		fprintf(fp,"TEST");
    		fclose(fp);
    	}
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    32

    thanks everyone

    Salem's suggestion did the trick. Thanks everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  2. Opening Multiple Files in sequence
    By wujiajun in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2006, 08:47 PM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 11:47 AM
  5. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 10:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21