Writing a text file to an array?

This is a discussion on Writing a text file to an array? within the C Programming forums, part of the General Programming Boards category; Hi, I'm very new to programming and am taking an entry-level course in C. One of the homework problems we ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    Writing a text file to an array?

    Hi, I'm very new to programming and am taking an entry-level course in C. One of the homework problems we have is to take two text files, merge them into a third file, and sort the file alphabetically. I think that I've written a code to merge the files, although I haven't tested it yet because he hasn't given us the address of the files - once I have more of the code written I'll make my own files to test it. The part that I can't get is the sorting. I know how to sort arrays alphabetically, but I don't know how to get the file into an array in the first place. This is made harder by the fact that we don't know how long the list is (our program is supposed to work no matter what files he gives us). Here is what I have so far, can anybody give me some guidance on how to proceed? Thank you!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <io.h>
    
    main() {
    
    	FILE *fp1, *fp2, *fp3;
    
    //merge documents into one list (list names are placeholders)
    	fp1 = fopen(list1.txt, "r");
    	fp2 = fopen(list2.txt, "r");
    	fp3 = fopen(mergelist.txt, "w");
    	while (fgets(str, 80, fp1)){
    		fprintf("%s\n", str);
    		close(fp1);
    	}
    	while (fgets(str, 80, fp2)){
    		fprintf(%s\n", str);
    		close(fp2);
    	}
    	close(fp3)
    
    
    //count how many lines are in the new list
    	fp3 = fopen(mergelist.txt, "r")
    	int i, j, k;
    	i=0;
    	while(fgets(str, 80, fp3)){
    		i++;
    	}
    
    //make an array long enough to hold the entire list
    	float x[i];
    Once the text is an array, I'm planning on using this to sort it alphabetically:
    Code:
    for (j=0; j<i-1; j++) {
    		for(k=j+1; k<i; k++) {
    			if(strcmp(x[j], x[k])!=0 swap(&x[j], &x[k]);
    		}
    	}
    }
    
    int swap(char x[], char y[]) {
    	char tmp[20];
    	strcpy(tmp, x);
    	strcpy(x, y);
    	strcpy(y, tmp);
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Not
    != 0,
    but
    > 0
    then swap.

    The line of text can go right into your array with
    Code:
    while((fgets(array2d[row++], sizeof(array2d[row]), filepointer)) != NULL) {
    }
    Or, if you need to break out each word into your array row, then use strtok() on it, (see the other post on this page about text words into an array for more on that).

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Oh, thank you, I didn't notice that. Were there any other glaring errors?

    I also think I came up with a way to write the file to an array, since I didn't understand the strtok explanations I found. Do you think this would work? (I would delete the piece of code counting lines and making an array.)

    Code:
    fp3 = fopen(mergelist.txt, "r")
    int i=0;
    while(fscanf(fp3, "%s", str)){
    	array[i] = str;
    	i++;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can't directly assign a string like that, in C. Happily, you don't need to do that, since fscanf() will do that for you.
    Code:
    while(fscanf(fp3, "%s", array[i]) {
    (If you ever have to do it, use a function like strcpy() )

    Is array a two dimension array? (ie: array[][])

    What are you going to do when fscanf() reaches the end of the file, and tries to return EOF (usually, -1)?

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Would it be

    Code:
    while(fscanf(fp3, "%s", array[i])!=NULL) {
    i++;
    }
    ?

    Sorry if I seem completely clueless, I really don't understand most of what our professor does so a lot of my programming is throwing things from the internet and books together and hoping that it works...

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, because fscanf() will return -1 on end of file. (EOF)

    What about replaicing != NULL with > 0? One great thing about programming, is that you can sometimes test these things out, for yourself. So give that a try.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    I'm still not getting it to work. When I try to compile, I get the error "warning: passing argument 1 of fprintf from incompatible pointer type" for the lines which read

    Code:
    fprintf("%s\n", str);
    Any idea what might be causing it?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You forgot the file pointer in the fprintf() statement.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Would

    Code:
    fprintf(fp1, "%s\n", str);
    be correct?

  10. #10
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    What does your current code look like?

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    rags_to_riches, here's my entire code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main() {
    
    	FILE *fp1, *fp2, *fp3;
    	char str[80];
    
    //merge documents into one list (list names are placeholders)
    	fp1 = fopen("list1.txt", "r");
    	fp2 = fopen("list2.txt", "r");
    	fp3 = fopen("mergelist.txt", "w");
    	while (fgets(str, 80, fp1)!=NULL){
    		fprintf("%s\n", str);
    		close(fp1);
    	}
    	while (fgets(str, 80, fp2)!=NULL){
    		fprintf(%s\n", str);
    		close(fp2);
    	}
    	close(fp3)
    
    
    //count how many lines are in the new list
    	fp3 = fopen("mergelist.txt", "r");
    	int i, j, k;
    	char tmp
    	i=0;
    	while(fgets(str, 80, fp3)!=NULL){
    		i++;
    	}
    	float array[i];
    	
    //put the list into an array
    	i=0;
    	while(fscanf(fp3, "%s", array[i])>0){
    		i++;
    	}
    
    
    //sort the array
    	for (j=0; j<i-1; j++) {
    		for(k=j+1; k<i; k++) {
    			if(strcmp(array[j], array[k])>0) {
    				tmp = array[j];
    				array[j] = array[k];
    				array[k] = tmp;
    			}
    		}
    	}
    }

  12. #12
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    Code:
    fp3 = fopen("mergelist.txt", "w");
        while (fgets(str, 80, fp1)!=NULL){
            fprintf("%s\n", str);
            close(fp1);
        }
    That's only going to get one line from fp1 because you're closing it after the first line is read. Same here, only with fp2:
    Code:
    while (fgets(str, 80, fp2)!=NULL){
            fprintf(%s\n", str);
            close(fp2);
        }
    Code:
    fprintf("%s\n", str);
    fprintf takes as its first argument a file pointer. Which one do you suppose you need to use in each case?

    Code:
    char tmp
    You're missing a semi-colon, and I don't think that's supposed to be a char. To what is tmp going to POINT?

    Code:
    float array[i];
    This is using non-standard C which may or may not be available to you depending upon your compiler.

    There's a big push for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read and store text file as an array
    By abotaha in forum C++ Programming
    Replies: 1
    Last Post: 08-02-2010, 08:57 PM
  2. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  3. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Text file to 2D Array PLEASE HELP!
    By lostboy101 in forum C Programming
    Replies: 0
    Last Post: 03-26-2002, 09:51 AM

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