Reading file using fscanf into array

This is a discussion on Reading file using fscanf into array within the C Programming forums, part of the General Programming Boards category; Hi all, I'm sure that I'm missing something silly, but either I'm not reading the file into the array correctly ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    12

    Reading file using fscanf into array

    Hi all,

    I'm sure that I'm missing something silly, but either I'm not reading the file into the array correctly or I'm not outputting it correctly. I can see my program looping from my printf "Data scanned:" output to the screen, but I don't think I've properly populated my array "hold".

    Any suggestions?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    #define		NUM_THREADS		10
    #define		MAX_LENGTH		10
    
    void *read_files(void *cnt);
    
    int main() 
    {
    	int cnt;
    	pthread_t p_thread[NUM_THREADS];
    	char file[10] = "file1";
    
    	for(cnt = 0; cnt < NUM_THREADS; cnt++)
    		/* Returns 0 on successful creation of the thread */
    		if(pthread_create(&p_thread[cnt], NULL, read_files, (void *)cnt) != 0)
    		{
    			fprintf(stderr, "Error creating the thread");
    		}
    	for(cnt = 0; cnt < NUM_THREADS; cnt++) 
    	{
    		/* Waits for p_thread[cnt] to finish. */
    		pthread_join(p_thread[cnt], NULL);
    	}
    	fprintf(stdout, "All threads completed.\n");
    	return 0;
    }
    
    void *read_files(void *cnt)
    {
    	size_t 	c = 0;		/* Counter variable */
    	int		d = 1;
    	int		n = 0;
    	char	hold[100];
    	char	temp[10];
    	char 	filename[10] = "file";
    	
    	FILE *datain;		/* File to be opened */
    
    	snprintf(filename, MAX_LENGTH, "%s%d", filename, cnt);	
    	printf("Filename = %s \n", filename);
    	/* Attempt to open file */
    	datain = fopen(filename, "r");
    
    	while (!feof(datain))
    	{
    		fscanf(datain, "%s", &hold[n]);                /* Not working */
    		printf("Data scanned: %s\n", hold[n]);    /* Not outputting hold var */
    		n++; 
    	}
    	/* Close the files */
    	fclose(datain);
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It seems you are trying to read a string into a single character:
    Code:
            char	hold[100];
    	while (!feof(datain))
    	{
    		fscanf(datain, "%s", &hold[n]);                /* Not working */
    		printf("Data scanned: %s\n", hold[n]);    /* Not outputting hold var */
    		n++; 
    	}
    If the input file looks like this:

    line one
    next line
    last line
    EOF

    "hold" will end up as:

    lnlast line

    Do you understand why that is?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    Ah, yes - I see this now.

    What I want to do is read the full file, word by word, and write this into my buffer (hold).

    Here I can have the program read through all the files, word by word, and output the word, but the end result - once I get these pieces figured out - is to create one file from the ten input files.

    Code:
    	while (fscanf(datain, "%s", hold) == 1)
    			{
    			printf("In the loop: %s\n", hold);
    			}
    I guess I could create another array and then assign hold to this each time it reads a word, but I bet there's a more efficient and cleaner method. I'll keep working on it.

    Thanks!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Maybe check that the function works for one file WITHOUT using threads to being with.

    Threads going wrong add a whole new dimension to the problems you'll be facing, so it helps to know that the code works beforehand.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a list of ints from file into an array
    By mesmer in forum C Programming
    Replies: 1
    Last Post: 11-10-2008, 05:45 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Replies: 5
    Last Post: 10-02-2005, 12:15 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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