Thread: C Program PLEASE HELP!

  1. #16
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    So, you're pretty close, fixed a few convention/other small issues:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define MAX 81
    
    
    FILE *fileOpen(void);
    int readFile(FILE* inputFile);
    
    
    int main (void)
    {
    	FILE *inputFile;
    	
    	inputFile = fileOpen();
    	
    	readFile(inputFile);
    	
    	fclose(inputFile); /* you need to do this */
    	
    	return 0;
    }
    
    
    FILE * fileOpen(void)
    {
    	FILE *inputFile;
    	char fileName[MAX]; /* no reason to make it = "" */
    	
    	printf("Please enter a file name.\n");
    	scanf("%s", fileName);
    
    
    	inputFile = fopen(fileName, "r"); /* because shoving everything onto one line isn't a good idea */
    	if (!inputFile) {
    		printf("Could not open the input file.\n");
    		exit(1); /* 100 isn't standard */
    	}
    	return inputFile;
    }
    
    
    int readFile(FILE* inputFile)
    {
    	char strng[MAX];
    
    
    	/* don't need extra stuff here */
    	
    	while(fgets(strng, sizeof(strng), inputFile)) 
    		puts(strng); /* don't use stdin, just use stdout (default for puts) */
    	
    	return 0;
    
    
    }
    Last edited by memcpy; 02-23-2012 at 07:12 PM.

  2. #17
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by anduril462 View Post
    Much better. Change fileOpen back to the way it was, where you passed in fileName. Sorry we confused you, I didn't realize your assignment/teacher stated it had to take that parameter. The other problems are in readFile:
    Code:
        while(fgets(strng, sizeof(strng), inputFile)){
            fputs(strng, stdin);
            return 1;
        }
    That is an input file, you can't put (fputs) to stdin. You have to fputs to stdout, so it will show up on the screen.
    That line will cause you to only read one line from the file and echo it back. If that is what you want, change the while to an equivalent if statement. If you want it to read the whole file, remove that line.

    Ok, I changed fileOpen back to the way it was. Also, I changed fputs to fgets so that part now looks like this:
    fgets(strng, len, stdin);
    I got rid of return 1.

    -One problem that I have right now. When I run the program and it prompts for the file name and I type it in, it just goes to the next line, CONTINUOUSLY. I press enter, and it moves on to the next line. Not sure how it happened... Let me know if you need me to post my current code to figure it out.

  3. #18
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by memcpy View Post
    So, you're pretty close, fixed a few convention/other small issues:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define MAX 81
    
    
    FILE *fileOpen(void);
    int readFile(FILE* inputFile);
    
    
    int main (void)
    {
        FILE *inputFile;
        
        inputFile = fileOpen();
        
        readFile(inputFile);
        
        fclose(inputFile); /* you need to do this */
        
        return 0;
    }
    
    
    FILE * fileOpen(void)
    {
        FILE *inputFile;
        char fileName[MAX]; /* no reason to make it = "" */
        
        printf("Please enter a file name.\n");
        scanf("%s", fileName);
    
    
        inputFile = fopen(fileName, "r"); /* because shoving everything onto one line isn't a good idea */
        if (!inputFile) {
            printf("Could not open the input file.\n");
            exit(1); /* 100 isn't standard */
        }
        return inputFile;
    }
    
    
    int readFile(FILE* inputFile)
    {
        char strng[MAX];
    
    
        /* don't need extra stuff here */
        
        while(fgets(strng, sizeof(strng), inputFile)) 
            puts(strng); /* don't use stdin, just use stdout (default for puts) */
        
        return 0;
    
    
    }
    I edited most of what you changed. The part where I shoved everything on one line was because the book has it that way. But I changed it to your method since this is probably better coding.
    The 100 was also what was used in the book, but I changed it to 1.
    For the while loop, I already adjusted it to what anduril462 told me to do.
    I think I need the extra stuff cause its part of what the function is supposed to do... I think I posted the assignment somewhere in the beginning of the thread, but I'll repost it again if I need to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 09:50 PM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM