Help with C Please :)

This is a discussion on Help with C Please :) within the C Programming forums, part of the General Programming Boards category; Hey everyone, im a first time programmer, electrical engineer major at UCSD and well I have to learn C. I'm ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    Help with C Please :)

    Hey everyone, im a first time programmer, electrical engineer major at UCSD and well I have to learn C. I'm not really looking for answers, just ideas and help on my program, its my first assignment in programming ever and my professor is for lack of a better term, an Idiot. He can't explain how the program is supposed to work or what i should be trying to use in it so I've had to resort to getting help on the internet which is why im here.

    This is my first assignment, it is quite a beast if you've never touched code before, well at least to me its a beast. It pretty much says open .txt file which will list to you several .c file in which i must print out the first comment, count every line of comments, cound every blank line, and every line of actual coding. Take these numbers and write them into a new .txt file which needs to be organized.

    ______ Does the lab compile and start running?
    ______ Does the lab print to the screen: ECE 15 Lab 1, and then the name of each lab
    partner on a separate line?
    Partner names:____________________________________________ ______
    ______ Does the lab ask the user for the name of a file (the names file) to open, then
    accept that input?
    ______ Does the lab print to the screen the first line of text in the opened names file?
    ______ Does the lab then use that line as another filename to open (the program file)
    and print to the screen the first comment, /* */ or // type, it finds?
    ______ Does the lab loop through the program file, printing to the screen all comments
    found?
    ______ Does the lab print to the screen the total number of comments, comment characters,
    comment lines, and non-blank non-comment lines found in the file?
    ______ Does the lab also record the program filename and the above information to a
    text file: results.txt, all on a single line?
    ______ Does the program correctly loop over all program files in the names file, printing
    their results to the screen and appending them to results.txt.
    ______ Are the data in results.txt formatted in neat vertical columns?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main(int argc, char *argv[])
    {
    	printf("ECE 15 Lab 1\n\nJoel Trost\n\n");
    	
    	int i = 0;
    	int counT;
    	char s[81];
    	char SF[81];
    	FILE *fRead, *Read2;
    
    	printf("Please Input A Proper File Name:  ");
    	
    	scanf("%s", s);
    	fRead = fopen(s, "r");
    	fgets(s, 81, fRead);
    	printf(s);
    
    	Read2 = fopen('%s', "r");
    
    	Do
    	{
    		if(Read2[i[ == '/' && Read2[i++] == '/')
    		{
    			fgets(SF, 81, Read2);
    			printf(s);
    		{
    		if(Read2[i] == '/' && Read2[i++] == '*')
    		{
    			fgets(SF, 81, Read2);
    			printf(s);
    		}
    		else if(Read2[i] == '*' && Read2[i++] == '/')
    			Break 0;
    	While(Read2[i] != 'null')
    			
    	}
    This is my code and as you can imagine it doesnt work, if you comment it out before Read2 is trying to be set to read the imbedded files in the .txt you can get input which was pretty simple. With the loop below i was trying to read through the imbedded file and print out the comments and of course it doesnt work.

    My main problem is i dont know how the logic works and have had difficulty in finding examples of reading files in C mainly due to no one actually does read files with C, well so im told. If anyone can please help me, explain some idea of what i should be using or what command lines i should toy around with any and all help would be much appreciated. I'm here to learn and being completely clueless as to how the entire process works im completely lost.

    Thanks again.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    First of all, C is a Case senSitive language. There is no command Do, however, do. Likewise While -> while, Break. . . etc.

    fopen() returns a FILE pointer. This is not an array. To read through a file you'll need to use something like fgets() or fgetc().

    You can find manual (man) pages about this via the web, or if you are on a *nix box, you can simply type in `man fread` for more help.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main(int argc, char *argv[])
    {
    	printf("ECE 15 Lab 1\n\nJoel Trost\n\n");
    	
    	int i;
    	int countComChar = 2;
    	int countComment = 0;
    	int countComLine = 0;
    	/*int countCode;
    	int countBlank;*/
    	int letters;
    	int commentFlag = 0;
    	char s[81];
    	char SF[81];
    	FILE *fRead, *Read2;
    
    	printf("Please Input A Proper File Name:  ");
    	
    	//fscanf(fRead, "%s", s);
    	scanf("%s", s);
    	fRead = fopen(s, "r");
    	/*while( (letters = getc(fRead)) != '\n')
    	{
    		s[i] = (char)letters;
    		i++;
    	}*/
    	fgets(s, 81, fRead);
    	printf(s);
    	printf("\n");
    
    	Read2 = fopen(s, "r");
    
    	fgets(SF, 81, Read2);
    
    	while( (letters = getc(Read2)) != EOF)
    	{
    		if(letters == '/') 
    		{
    			letters = getc(Read2);
    			if( letters == '/')
    			{
    				countComLine++;
    				countComment++;
    				printf("//");
    				while( (letters = getc(Read2)) != '\n' ) 
    				{
    					printf("%c", letters);
    					countComChar++;
    				}
    				printf("\n");
    			}
    			else if(letters == '*') 
    			{
    				countComment++;
    				printf("/*");
    				while( 1) 
    				{
    					letters = getc(Read2);
    					countComChar++;
    					printf("%c", letters);
    					if(letters == '\n')
    					{
    						countComLine++;
    					}
    					if( commentFlag && letters == '/') 
    					{
    						countComLine++;
    						break;
    					}
     
    					if( letters == '*')
    					{
    						commentFlag = 1;
    					}
    					else {
    			
    						commentFlag = 0;
    					}
    				}
    				printf("\n");
    			}
    		}
    	}
    	printf("Total Number of Comments:\n", countComment, "\n");
    	printf("Total Number of Comment Lines:\n", countComLine, "\n");
    	printf("Total Number of Comment Chararcters:\n", countComChar, "\n");
    
    
    	return 1;	
    }
    Ok, hah, ive worked on my code quite a bit, thanks so much kennedy, helped a bit. But now im having a problem with my files, it opens the text file and reads and prints the comments in the first file that the .txt file refers to. My next problem is i dont know how to make it not read the newline charactors in the txt file because it ........s up and can't understand that there is a new file that it needs to read on the next line, and for some reason it isnt printing out the integer values for comment counting that i have going on. Thanks again guys.

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    printf("Total Number of Comments:\n", countComment, "\n");
    printf("Total Number of Comment Lines:\n", countComLine, "\n");
    printf("Total Number of Comment Chararcters:\n", countComChar, "\n");
    You migth want to put some conversion characters in there if you actually want to see some output. Something like :

    Code:
    printf("Total Number of Comments: %d\n", countComment);
    Also, there's a little something wrong with your logic. Your code would interpret /*adsdad*dgs/ as a comment, even thought it isn't (At least not until you encounter another */).
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

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