Thread: file io question

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    6

    file io question

    Hi all,

    In the following code i am trying to give the user the option to print to a chosen text file if they choose to. I have been reading up on pointers and file io and am still lost.

    Any help offered would be greatly appreciated.

    thanks in advance.
    Mike


    Code:
    #include <stdio.h>
    #include <string.h>
    
    void mysort(char word[][100], int size);
    
    int  i, a, x=0, j=0;
    
    void main()
    {
    char word[10][100];
    	printf("this program will sort up to 10 words in alphabetical order\n");
    	printf("if you have fewer than 10 words enter <ctrl> z twice\n\n");
    	for(a=0; a<10; a++)
    		{
    		printf("enter a word\n");
    		
    		if(scanf("%s", word[x]) != 1) break;
    		x=x+1;
    		j=x;
    		}
    		x=0;
    
    	printf("original words\n--------------\n");
    	for (i=0; i<j; i++)
    		{
    		printf("%s \n", word[i]);
    		}
    
    	mysort (word, j);
    	printf("\nsorted words\n------------\n");
    	for (i = 0; i <j; i++)
    		{
    		printf("%s \n", word[i]);
    		}
    
    }
    void mysort(char word[][100], int size)
    {
    	int pass, indx;
    	char hold[100];
    	for(pass = 0; pass < size - 1 ; pass++)
    		{
    		for(indx = 0; indx < size - pass - 1; indx++)
    			{
    			if(strcmp(word[indx], word[indx + 1]) >0)
    				{
    				strcpy(hold, word[indx]);
    				strcpy(word[indx], word[indx +1]);
    				strcpy(word[indx + 1], hold);
    				}
    			}
    		}	
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Here is a small sample program that prompts the user for a file name and prints "Hello World" to that file. It shouldn't take much to add this functionality to what you've got.

    Code:
    #include <stdio.h>
    
    int main (void) {
       char file_name[20];
       FILE * output;
    
       printf ("Enter a file name: ");
    
       scanf ("&#37;s", file_name);
    
       output = fopen (file_name, "w");
    
       fprintf (output, "Hello World!\n");
    
       return 0;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you use scanf() in that manner, you can't accept a filename with spaces. If you wish to allow spaces, either use fgets() (and trim off the newline char) or change scanf() to accept more appropriate input.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Several notes

    1. void main should be int main
    2. You don't need global vars - make them local in the main
    3. You don't need both x and j - you can change the progem to use only one of them
    4. Give your vars meaningfull names - not x, but wordCount for example
    5. x=x+1 most peaple will write x++, it is more common notation for the same thing
    6. You should work on your indentation - it will save you from some mistakes in the future
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    6
    Thanks for you input guys,

    Seems to be working. Now just need to figure out fgets().

    Cheers,

    Mike

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char buffer[BUFSIZ];
    	size_t index;
    
    	printf("Enter a string: ");
    	fflush(stdout);
    	fgets(buffer,sizeof(buffer),stdin);
    	index = strlen(buffer) - 1;
    	if(buffer[index] == '\n')
    	{
    		buffer[index] = '\0';
    	}
    	printf("You entered: <%s>\n",buffer);
    
    	return 0;
    }

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Erhm . . . just don't type EOF (^Z for Windows) on a blank line. You'd be accessing buffer[(size_t)-1], which is almost certainly going to result in a segmentation fault. You could fix that by checking the return value of fgets(): fgets() returns NULL if it encountered EOF or another error before it stored any characters in the array.

    I like this version:
    Code:
    #include <stdio.h>  /* for fgets(), printf(), puts(), and BUFSIZ */
    #include <string.h>  /* for strchr() */
    
    int main() {
        char buffer[BUFSIZ], *p;
    
        if(fgets(buffer, sizeof buffer, stdin)) {
            if((p = strchr(buffer, '\n'))) *p = 0;
            printf("You entered [&#37;s]\n", buffer);
        }
        else puts("No string entered");
    
        return 0;
    }
    There are many different ways to strip the newline from a string created by fgets(). These are just two of them.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM