C Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dineshchand
    New Member
    • Nov 2006
    • 19

    C Problem

    /*# ReadRealyLongTe xtLine() needs to be written (by you). The text line could be very very very very long!!! upto 30K characters or more than this
    # There is a logical error in the code – you need to find it and fix it.*/

    /***
    PrintX.c
    Prints the file C:\X.TXT line by line.
    */

    char * ReadRealyLongTe xtLine(File* p_fhInFile)
    {

    }

    void PrintX(void)
    {

    File* fhInFile;

    char *strLineBuf;
    fhInFile = fopen(“c:\\x.tx t”, “r”);

    if(fhInfile)

    {

    while( (strLineBuf = ReadRealyLongTe xtLine(fhInFile )) != NULL)

    printf(“%s”, strLineBuf);

    fclose(fhInFile );

    }

    else

    printf(“\r\nCou ld not find C:\\X.TXT!!”);



    }
    int main()

    {

    PrintX();

    }

    /* only FUNCTION readrealylongte xtline() needs to be written and the logical problem needs to be figured out
    plz do post ur replies .
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by dineshchand
    /*# ReadRealyLongTe xtLine() needs to be written (by you). The text line could be very very very very long!!! upto 30K characters or more than this
    # There is a logical error in the code – you need to find it and fix it.*/

    /***
    PrintX.c
    Prints the file C:\X.TXT line by line.
    */

    char * ReadRealyLongTe xtLine(File* p_fhInFile)
    {

    }

    void PrintX(void)
    {

    File* fhInFile;

    char *strLineBuf;
    fhInFile = fopen(“c:\\x.tx t”, “r”);

    if(fhInfile)

    {

    while( (strLineBuf = ReadRealyLongTe xtLine(fhInFile )) != NULL)

    printf(“%s”, strLineBuf);

    fclose(fhInFile );

    }

    else

    printf(“\r\nCou ld not find C:\\X.TXT!!”);



    }
    int main()

    {

    PrintX();

    }

    /* only FUNCTION readrealylongte xtline() needs to be written and the logical problem needs to be figured out
    plz do post ur replies .
    Hi. I think this part:
    while( (strLineBuf = ReadRealyLongTe xtLine(fhInFile )) != NULL)

    is a logical error as an assignment operation will always evaluate to true and never to NULL

    this part
    if(fhInfile)
    is a syntax error (typo maybe)

    Comment

    • dineshchand
      New Member
      • Nov 2006
      • 19

      #3
      no i think bug is at some other place , also u need to write the function ReadReallyLongT extLine()

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Originally posted by dineshchand
        no i think bug is at some other place , also u need to write the function ReadReallyLongT extLine()
        this snippet works. Perhaps you can work out the answer to your problem from this
        Code:
        #include "stdafx.h"
        #include <iostream>
        #include <fstream>
        #include <string>
        using namespace std;
        
        string inputtextfile(ifstream &pt)
        {
        	string buff;
        	getline(pt, buff);
        	return buff;
        }
        
        int main(int argc, char* argv[])
        {
        	string s;
        	ifstream f( "c:\\x.txt" );
        
        	if(!f) {
        	cerr << "Couldn't open file\n";
        	}
        
        	while (f) {
        		s = inputtextfile(f);
        		cout << s << '\n';
        	}
        
        	cout << endl;
        	return 0;
        }

        Comment

        • dineshchand
          New Member
          • Nov 2006
          • 19

          #5
          no answer has to be in that form only as in the original question
          same ReadRealyLongTe xtLine() function has to be implemented and with same return arguments as stated , thanks for this reply but was not required

          Comment

          • willakawill
            Top Contributor
            • Oct 2006
            • 1646

            #6
            Originally posted by dineshchand
            no answer has to be in that form only as in the original question
            same ReadRealyLongTe xtLine() function has to be implemented and with same return arguments as stated , thanks for this reply but was not required
            You're welcome and you have to do some work.
            When someone asks me to complete a project where they do not do any work I charge them $50 per hour. I take checks and PayPal :)

            Comment

            • svsandeep
              New Member
              • Nov 2006
              • 15

              #7
              Originally posted by dineshchand
              /*# ReadRealyLongTe xtLine() needs to be written (by you). The text line could be very very very very long!!! upto 30K characters or more than this
              # There is a logical error in the code – you need to find it and fix it.*/

              /***
              PrintX.c
              Prints the file C:\X.TXT line by line.
              */

              char * ReadRealyLongTe xtLine(File* p_fhInFile)
              {

              }

              void PrintX(void)
              {

              File* fhInFile;

              char *strLineBuf;
              fhInFile = fopen(“c:\\x.tx t”, “r”);

              if(fhInfile)

              {

              while( (strLineBuf = ReadRealyLongTe xtLine(fhInFile )) != NULL)

              printf(“%s”, strLineBuf);

              fclose(fhInFile );

              }

              else

              printf(“\r\nCou ld not find C:\\X.TXT!!”);



              }
              int main()

              {

              PrintX();

              }

              /* only FUNCTION readrealylongte xtline() needs to be written and the logical problem needs to be figured out
              plz do post ur replies .


              Dude,

              #include <stdio.h>
              #include <stdlib.h>
              #include <iostream.h>

              //Global Declarations.
              struct iread
              {
              char c[];
              } *ptr;
              int getout=0;

              char* ReadRealyLongTe xtLine(FILE *p_fhInFile)
              {
              char ch;

              //used to get out of the program once EOF has reached

              if (getout == 1)
              return NULL;

              //32 kb allocation. Increase this if you want more. Nothing related to exception handling is taken care here. What if we cant allocate the
              //Memory?? so dude do it yourself..

              ptr = (iread*) malloc(32768);

              int count = 0;
              while(p_fhInFil e != NULL )
              {
              ch = fgetc(p_fhInFil e);
              ptr->c[count]=ch;

              //Found the end of line throw the entire line back to the parent function

              if (ptr->c[count] == '\n')
              return ptr->c;

              //This in other words is useful if we have reached the last line
              //(which will not have \n at the end of line)

              if ( ch == EOF )
              {
              getout = 1;
              return ptr->c;
              }

              count++;
              }
              }


              void PrintX(void)
              {
              FILE *fhInFile;
              char *strLineBuf;
              fhInFile = fopen("c:/X.TXT", "r");

              if(fhInFile)
              {
              while( (strLineBuf = ReadRealyLongTe xtLine(fhInFile )) != NULL)
              {
              printf("%s", strLineBuf);
              //The below two statements are for taking the memory back from ptr's.
              ptr=NULL;
              free(ptr);
              }
              fclose(fhInFile );
              }
              else
              printf("\r\nCou ld not find C:\\X.TXT!!");
              }
              int main()
              {
              PrintX();
              }


              You can sure improve the above program ;)

              Regards,
              ShaggY@FtF

              Comment

              • dineshchand
                New Member
                • Nov 2006
                • 19

                #8
                hi

                ur program is good but lost of resources are wasted eg- consider when the no. of characters in the line r less i.e . 10 or so

                Thats the main area where u function has to handle resources .

                Hope u look into it.

                Comment

                • gurutheexpert
                  New Member
                  • Nov 2006
                  • 8

                  #9
                  Hi,
                  This is my first post. I have found a solution . Any suggestions are welcome.


                  #include <stdio.h>
                  #include <memory.h>

                  char * ReadRealyLongTe xtLine(FILE * p_fhInFile)
                  {
                  // status variable to decide whether to continue reading or not
                  static bool bContinue = true;

                  // create a buffer of suitabele size in my case i took 1000
                  static char *pVeryVeryLongT extLine = new char [1000];
                  memset (pVeryVeryLongT extLine,'\0',10 00);

                  char newChar;
                  char * TempPtrToTextLi ne = pVeryVeryLongTe xtLine;
                  int ncReadThisTime = 0;


                  // check if we need to continue reading or not
                  if (!bContinue)
                  {
                  delete pVeryVeryLongTe xtLine;
                  return NULL;
                  }

                  // continue reading file until end of file reached
                  // or a line ends or buffer capcity is over
                  while ( (newChar != EOF) && ( newChar != '\n') && (ncReadThisTime != 999) )
                  {
                  newChar = getc(p_fhInFile );

                  *TempPtrToTextL ine++ = newChar;
                  ncReadThisTime+ +;
                  }

                  if (ncReadThisTime == 999)
                  bContinue = true;
                  else
                  bContinue = false;

                  // if line ended or end of file detected
                  // return the characers read this time and next time return NULL
                  return pVeryVeryLongTe xtLine;
                  }

                  void PrintX(void)
                  {

                  FILE* fhInFile;

                  char *strLineBuf;
                  fhInFile = fopen("x.txt", "r");

                  if(fhInFile)

                  {

                  while( (strLineBuf = ReadRealyLongTe xtLine(fhInFile )) != NULL)

                  printf("%s", strLineBuf);

                  fclose(fhInFile );

                  }

                  else

                  printf("\r\nCou ld not find C:\\X.TXT!!");


                  }
                  int main()

                  {

                  PrintX();
                  return 1;
                  }

                  Comment

                  • dineshchand
                    New Member
                    • Nov 2006
                    • 19

                    #10
                    i looked into ur code and i found out the following things
                    1. what if the line is greater than 1000 characters ,e g if it is 1050 characters long than u have to return whole characters in the in the same line ....

                    Comment

                    • gurutheexpert
                      New Member
                      • Nov 2006
                      • 8

                      #11
                      Hi,

                      If line contains more than 1000 chars , then it will return the chars in chunks of 999 at one time until the line ends or end of file is reached.

                      I think u haven't noticed the bool variable and comment written there. it is a static variable whose status will change to "false" only when new line starts or end of file reached and then it will return NULL which will terminate ur while int Print ();

                      I hope i cleared ur doubt. u can test and verify the same as i have done.

                      Comment

                      • gurutheexpert
                        New Member
                        • Nov 2006
                        • 8

                        #12
                        Hi,

                        One more thinhg ,your while loop is to decide when to stop reading . Until ReadRealyLongTe xtLine() doesn't return NULL , it isn't going to terminate.

                        Here ReadRealyLongTe xtLine() returns NULL only when whole line is read.
                        Only thing is that you have a slight trade of betwwen execution time ie. ReadRealyLongTe xtLine function will be called more times , if line size is large.

                        But it is upto you to decide whether you want time efficiency or memory.

                        Comment

                        • dineshchand
                          New Member
                          • Nov 2006
                          • 19

                          #13
                          Originally posted by gurutheexpert
                          Hi,

                          One more thinhg ,your while loop is to decide when to stop reading . Until ReadRealyLongTe xtLine() doesn't return NULL , it isn't going to terminate.

                          Here ReadRealyLongTe xtLine() returns NULL only when whole line is read.
                          Only thing is that you have a slight trade of betwwen execution time ie. ReadRealyLongTe xtLine function will be called more times , if line size is large.

                          But it is upto you to decide whether you want time efficiency or memory.
                          hi
                          yaar thanks for the reply and so much of effort u r giving for this .
                          yes i understood ur logic (u r calling the ReadRealyLongLi ne function for every 999 characyers read ) but there r some compile time errors
                          Will u plz look into this ?

                          Comment

                          • gurutheexpert
                            New Member
                            • Nov 2006
                            • 8

                            #14
                            Originally posted by dineshchand
                            hi
                            yaar thanks for the reply and so much of effort u r giving for this .
                            yes i understood ur logic (u r calling the ReadRealyLongLi ne function for every 999 characyers read ) but there r some compile time errors
                            Will u plz look into this ?

                            hi,
                            Sure. What errors ,as far as I know , there are no errors in this program. I have tested it . Let me know .

                            Comment

                            • dineshchand
                              New Member
                              • Nov 2006
                              • 19

                              #15
                              Originally posted by gurutheexpert
                              hi,
                              Sure. What errors ,as far as I know , there are no errors in this program. I have tested it . Let me know .
                              Thanks for ur effort .... there was some petty errors ....i highly appreciate ur logic ...
                              Thanks once more ..

                              Comment

                              Working...