Thread: The project programming C problem, please help seriously>.<

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    4

    The project programming C problem, please help seriously>.<

    Our college changes 18 weeks semester to 16 semester, so our CS professor cannot finish teaching the last important chapter which is related with my problem.

    This is program C problem

    Anyone can help me with this problem, please!!!!!!!!!
    This is the problem:
    3. Several input text files have been provided as input to your program.
    a) Write a function to combine these files into a single file.
    b) Write a function to take care of multiple spaces and replace each occurrence of a multiple spaces with a single space.
    c) Write a function to examine the words in your text file and count the number of words. Words are separated by white spaces i.e. space, newlines or tabs.
    d) Write a function to count the word length in the file. Do not count punctuation characters when determining word length. Report on the screen the shortest and longest words.
    Test all the above functions. Try it first by using your own text files" Myfile1" and" Myfile2" (write two short text files, about ten words each). Then use the data posted http://www.mtsac.edu/rvhpop/proj4-110.txt
    the hint is that it is similar to this code:

    Code:
    #include <stdio.h>
    
    main()
    {
    FILE
    *inptr, *outptr;
    int 
    c,empno;
    float
    pay;
    char
    empname[20];
    /************************************************** **************/
    /* open the input and the temporary files, with error checking */
    /************************************************** **************/
    inptr = fopen("payrates", "r");
    if (inptr == NULL)
    {
    printf("Problem opening file \"payrates\" \n");
    exit(1);
    }
    outptr = fopen("tempfile","w");
    if (outptr == NULL)
    {
    printf("Problem opening output file...\n");
    exit(1);
    }
    /************************************************** **************/
    /* read the file, update the pay field, write to the temp file */
    /************************************************** **************/
    
    c = fscanf(inptr,"%d %s %f",&empno, empname, &pay);
    while (c != EOF)
    {
    pay = pay*1.05;
    fprintf(outptr,"%4d %20s %6.2f\n",empno,empname,pay);
    c = fscanf(inptr,"%d %s %f",&empno, empname, &pay);
    }
    /************************************************** **************/
    /* make sure you close the files, otherwise remove and rename */
    /* will not work correctly. */
    /************************************************** **************/
    fclose(inptr);
    fclose(outptr);
    /************************************************** **************/
    /* remove the old "payrates" file, rename "tempfile" "payrates" */
    /* note the error checking. */
    /************************************************** **************/
    /* USE remove ON THE MAC SYSTEMS, unlink ON UNIX */
    /*if((remove("payrates")) == -1) */
    if((unlink("payrates")) == -1)
    {
    printf("remove didn't work...\n");
    }
    if((rename("tempfile","payrates")) != 0)
    {
    printf("rename error...\n");
    }
    }
    Pleas help
    My email: <<snipped - this isn't a homework to email delivery service>>

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Apart from poor indentation, what have you learnt?

    > a) Write a function to combine these files into a single file.
    If you haven't learnt how to copy a file in 16 weeks, then there isn't a lot we can do.

    At least have a go at solving this part of the problem, then we can work on the statistics later on.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    The copy file chapter is the last chapter we have but the professor just said it was not hard last week. After that we still need to work on lab and review final,so he just give us this problem without explaination. We only have one day class experience with copying file and something like input or output. The 16weeks are not all about copying files.

    Please help me out.................Thx..........@.@

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    1. Open a new file for writing.
    2. Open one of the existing files for reading.
    3. In a loop, read the existing file's contents into a buffer. Write this buffer to the new file.
    4. Once you have reached the end of the existing file, open the other existing file and do the same thing.
    5. Close all files.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    I open the new file,but how to do the second step.
    This is my first code:
    Code:
    #include <stdio.h>
    
    main()
    {
    	char
    		filename[100];
    	int
    		c;
    	FILE
    		*inptr;
    
    	printf("\nPlease enter a file name: ");
    	scanf("%s",filename);
    	printf("The file name you entered was %s\n",filename);
    	inptr = fopen(filename,"w+");
    	if(inptr == NULL)
    	{
    		printf("problems opening file...\n");
    	}
    	}
    How to start to read the these files.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    alright, so now u have opened the file for writing.

    Now open the file which u need to read and combine all those contents into a single which you opened for witting.

    So the next step of your is to open a file which u need to read. and here is a sample code which u can refer for reading contents from the file.

    Code:
    #include<stdio.h>
    
    int main()
    {
        char str[BUFSIZ];
        FILE *fp;
        
        if((fp=fopen("test.txt","r")) == NULL)
        {
            printf("Error: File cannot be opened\n");
            getchar();
            return 1;
        }
        else
        {
            while(fgets(str,BUFSIZ,fp) != NULL)
                    printf("%s\n",str);
        }
        
        getchar();
        return 0;
    }
    You can read a file char by char or line by line. the above solution is Reading files line by line.

    ssharish2005
    Last edited by ssharish2005; 12-03-2006 at 05:28 AM.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    Thanks a lot~~ssharish2005
    But I still confuse with the step 2 and step 3
    In a loop, read the existing file's contents into a buffer. Write this buffer to the new file.
    So I simply put what ssharish2005 said with my first code:
    Code:
    #include <stdio.h>
    
    main()
    {
    		FILE
    		*inptr,*outptr;
    	char
    		filename[100]; char str[BUFSIZ];
    	int
    		c;
    	{
    	printf("\nPlease enter a file name: ");
    	scanf("%s",filename);
    	printf("The file name you entered was %s\n",filename);
    	inptr = fopen(filename,"w+");
    	if(inptr == NULL)
    	{
    	 if((outptr=fopen("Myfile1.txt","r")) == NULL)
    	 if((outptr=fopen("Myfile2.txt","r")) == NULL)
    	 if((outptr=fopen("Myfile3.txt","r")) == NULL)
    	 {
    		  printf("Error: File cannot be opened\n");
    		  getchar();
    		  return 1;
    	 }
    	 else
    	 {
    		  while(fgets(str,BUFSIZ,stdin) != NULL)
    					 printf("%s\n",str);
    	 }
    
    	 getchar();
    	 return 0;
    	 }
    }
    }
    I put three reading code, then I don't know how to continue.......
    I think it's very weird

    My professor's website has three files I need to combine,so I put reading Myfile1~Myfile3
    http://www.mtsac.edu/rvhpop/proj4-110.txt

    Please help me out with this problem because I have difficulty to understand the CS course
    Language problem maybe.

    Please!!!!!!

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    while(fgets(str,BUFSIZ,stdin) != NULL)
    Change stdin to outptr

    ohh mna, u will have to look through some syntax error from you code. Did u compile your program.

    ssharish2005

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. in general inptr name is used for input and outsomething for output. Your use of variable names is a little bit confusing
    2.
    Code:
    if(inptr == NULL)
    	{
    if fopen failed you are not going anyway, so you should rethink your condition
    3.
    Code:
    	 if((outptr=fopen("Myfile1.txt","r")) == NULL)
    	 if((outptr=fopen("Myfile2.txt","r")) == NULL)
    	 if((outptr=fopen("Myfile3.txt","r")) == NULL)
    What exactly do you want to achieve with this commands sequence?
    4. printf("%s\n",str); - I thought you want to save strings into file not print them on the screen?
    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

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    printf("%s\n",str); - I thought you want to save strings into file not print them on the screen?
    It looks like he just copied my code

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. How to add a file to a project in bloodshed.
    By Smeep in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 09:29 PM
  3. Project details: Dedicated
    By Stack Overflow in forum Projects and Job Recruitment
    Replies: 9
    Last Post: 02-22-2005, 03:10 PM
  4. Operating System Project
    By Pete in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 07-15-2004, 09:33 AM