Thread: FILE pointer problem

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    73

    FILE pointer problem

    Read the output of a file as

    ABCDEFGHIFSFSA
    1242487132
    FSFSA
    *


    a) Print the output as

    *
    FSFSA
    1242487132
    ABCDEFGHIFSFSA

    b) Print the output as
    *
    AFFSS
    1122234478
    AABCDEFFFGHISS

    HELP NEEDED URGENT!!!!!

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    What have you done so far?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What have you done so far?
    Well they posted it twice
    FILE pointer problem
    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.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    73

    its not compiling......

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    int main()
    {
    	FILE *fp1,*fpo;
    	char s[20][100],ch;
    	int i,j;
    	fp1=fopen("pr31.c","w");
    	if(fp1==NULL)
    	{
    		printf("cannot open file");
    	}
    	while(strlen(gets(s))>0)
    	{
    		fputs(s,fp1);
    		fputs("\n",fp1);
    	}
    	printf("file closed\n\n");
    	fpo=fopen("pr32.c","w");
    	ch=fgetc(fp1);
    	i=j=0;
    	while(!feof(fp1))
    	{
    		if(ch=='\n')
    		{
    			s[i][j]='\0';
    			i++;
    			j=0;
    		}
    		else
    		{
    			s[i][j]=ch;
    			j++;
    		}
    		ch=fgetc(fp1);
    	}
    	for(j=0;j<i;j++)
    		printf("%c",s[j]);
    	getch();
    	return 0;
    }

  5. #5
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    its not compiling......
    Are you sure ? it compiles for me.
    whats the error message you are getting ?

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    my prog is writing to the file and reading from it the array of strings...
    though it is compiling i am not getting the output correctly

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Code:
    printf("%c",s[j]);
    s is declared as a 2D array, and while printing it you're using s[j] which will give you the corresponding addresses not the chars. You should be doing s[i][j]. Although I haven't read the whole code. There might be some other errors too.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    i am compiling in turbo c++4.5
    i m getting general protection exception

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Have you tried what I said?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    i need it urgent pls someone post me whole correct code

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    i am compiling in turbo c++4.5
    i m getting general protection exception

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    =>BEN10
    yes i did but still the general protection exception is occuring

  13. #13
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    printf("file closed\n\n");
    Umm, you do know you actually have to close the file right ?

    Code:
    fclose(fp1);
    Also, change your while loop to this and remove the fgetc at the bottom-

    Code:
    while((ch=fgetc(fp1)) != EOF)
    Finally, you need to close your second file after your while loop-
    Code:
    fclose(fpo);

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Get back to your book and learn C properly. Why do you think it will work to read into a 2D array, for example?
    The next step would be to burn Turbo C++ and throw it out the window.
    Then get yourself a real IDE + compiler (try on here: SourceForge.net: Integrated Development Environment - cpwiki).
    And then, NEVER EVER use gets: SourceForge.net: Gets - cpwiki
    And finally, don't use feof as loop control; see faq.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    [insert]
    Code:
    At any rate, one can use something like this to replace gets(). #include <string.h> char * safegets(char * buffer, size_t count) { char * result = buffer; char * tail; if(buffer == NULL || count <= 0) result = NULL; else if(count == 1) *result = '\0'; else if(fgets(buffer, count, stdin) != NULL) { if( (tail = strchr(buffer, '\n')) != NULL) *tail = '\0'; } return result; } Note the return value of fgets() is checked. If fgets() returns NULL, it failed to read any input from stdin. This usually only happens if the user types End-Of-File, CTRL-Z on DOS and CTRL-D on UNIX, or the input was a file and the end of that file has been reached. Usually this can be ignored, but this function returns NULL if fgets() failed, just in case the caller wants to check it. As a side note, size_t is the proper type for the lengths of strings. Use fgets(), not gets().
    In the wiki page it says size_t is the proper type for the lengths of the strings. I didnt get what it means by that. Is it some identifier in c which is predefined to hold the lengths of the strings?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM