473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C Problem

/*# ReadRealyLongTextLine() 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 * ReadRealyLongTextLine(File* p_fhInFile)
{

}

void PrintX(void)
{

File* fhInFile;

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

if(fhInfile)

{

while( (strLineBuf = ReadRealyLongTextLine(fhInFile)) != NULL)

printf(“%s”, strLineBuf);

fclose(fhInFile);

}

else

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



}
int main()

{

PrintX();

}

/* only FUNCTION readrealylongtextline() needs to be written and the logical problem needs to be figured out
plz do post ur replies .
Nov 28 '06 #1
18 1725
willakawill
1,646 1GB
/*# ReadRealyLongTextLine() 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 * ReadRealyLongTextLine(File* p_fhInFile)
{

}

void PrintX(void)
{

File* fhInFile;

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

if(fhInfile)

{

while( (strLineBuf = ReadRealyLongTextLine(fhInFile)) != NULL)

printf(“%s”, strLineBuf);

fclose(fhInFile);

}

else

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



}
int main()

{

PrintX();

}

/* only FUNCTION readrealylongtextline() 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 = ReadRealyLongTextLine(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)
Nov 28 '06 #2
no i think bug is at some other place , also u need to write the function ReadReallyLongTextLine()
Nov 28 '06 #3
willakawill
1,646 1GB
no i think bug is at some other place , also u need to write the function ReadReallyLongTextLine()
this snippet works. Perhaps you can work out the answer to your problem from this
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. string inputtextfile(ifstream &pt)
  8. {
  9.     string buff;
  10.     getline(pt, buff);
  11.     return buff;
  12. }
  13.  
  14. int main(int argc, char* argv[])
  15. {
  16.     string s;
  17.     ifstream f( "c:\\x.txt" );
  18.  
  19.     if(!f) {
  20.     cerr << "Couldn't open file\n";
  21.     }
  22.  
  23.     while (f) {
  24.         s = inputtextfile(f);
  25.         cout << s << '\n';
  26.     }
  27.  
  28.     cout << endl;
  29.     return 0;
  30. }
  31.  
Nov 28 '06 #4
no answer has to be in that form only as in the original question
same ReadRealyLongTextLine() function has to be implemented and with same return arguments as stated , thanks for this reply but was not required
Nov 29 '06 #5
willakawill
1,646 1GB
no answer has to be in that form only as in the original question
same ReadRealyLongTextLine() 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 :)
Nov 29 '06 #6
/*# ReadRealyLongTextLine() 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 * ReadRealyLongTextLine(File* p_fhInFile)
{

}

void PrintX(void)
{

File* fhInFile;

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

if(fhInfile)

{

while( (strLineBuf = ReadRealyLongTextLine(fhInFile)) != NULL)

printf(“%s”, strLineBuf);

fclose(fhInFile);

}

else

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



}
int main()

{

PrintX();

}

/* only FUNCTION readrealylongtextline() 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* ReadRealyLongTextLine(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_fhInFile != NULL )
{
ch = fgetc(p_fhInFile);
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 = ReadRealyLongTextLine(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\nCould not find C:\\X.TXT!!");
}
int main()
{
PrintX();
}


You can sure improve the above program ;)

Regards,
ShaggY@FtF
Nov 29 '06 #7
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.
Nov 30 '06 #8
Hi,
This is my first post. I have found a solution . Any suggestions are welcome.


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

char * ReadRealyLongTextLine(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 *pVeryVeryLongTextLine = new char [1000];
memset (pVeryVeryLongTextLine,'\0',1000);

char newChar;
char * TempPtrToTextLine = pVeryVeryLongTextLine;
int ncReadThisTime = 0;


// check if we need to continue reading or not
if (!bContinue)
{
delete pVeryVeryLongTextLine;
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);

*TempPtrToTextLine++ = 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 pVeryVeryLongTextLine;
}

void PrintX(void)
{

FILE* fhInFile;

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

if(fhInFile)

{

while( (strLineBuf = ReadRealyLongTextLine(fhInFile)) != NULL)

printf("%s", strLineBuf);

fclose(fhInFile);

}

else

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


}
int main()

{

PrintX();
return 1;
}
Nov 30 '06 #9
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 ....
Dec 1 '06 #10
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.
Dec 1 '06 #11
Hi,

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

Here ReadRealyLongTextLine() returns NULL only when whole line is read.
Only thing is that you have a slight trade of betwwen execution time ie. ReadRealyLongTextLine 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.
Dec 1 '06 #12
Hi,

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

Here ReadRealyLongTextLine() returns NULL only when whole line is read.
Only thing is that you have a slight trade of betwwen execution time ie. ReadRealyLongTextLine 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 ReadRealyLongLine function for every 999 characyers read ) but there r some compile time errors
Will u plz look into this ?
Dec 4 '06 #13
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 ReadRealyLongLine 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 .
Dec 4 '06 #14
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 ..
Dec 4 '06 #15
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.
Dude,

Thx for the suggestion. Okie then we will do one thing, how about counting number of chr's the line (the line we are going to read) have? This can be easily done by using fgetc function and a silly counter. According to this number, then we can allocate the memory required and also move back the file pointer to the required position using some seek function. This way there will not be waste of so called resources..

Regards,
ShaggY@FtF
Dec 4 '06 #16
guys
there is a major modification because we need to return whole line from the text file at once .can u guys come up with something ..i m looking forward to ur solution ...
Dec 8 '06 #17
The ReadRealyLongTextLine() needs to return whole line of the text file at one go
Dec 8 '06 #18
guys
there is a major modification because we need to return whole line from the text file at once .can u guys come up with something ..i m looking forward to ur solution ...

oopsie,

Confused!! i think that is what my code do...

Regards,
ShaggY@FtF
Dec 11 '06 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
117
by: Peter Olcott | last post by:
www.halting-problem.com
18
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: =?Utf-8?B?am8uZWw=?= | last post by:
Hello All, I am developing an Input Methop (IM) for PocketPC / Windows Mobile (PPC/WM). On some devices the IM will not start. The IM appears in the IM-List but when it is selected from the...
1
by: sherifbk | last post by:
Problem description ============== - I have 4 clients and 1 server (SQL server) - 3 clients are Monitoring console 1 client is operation console - Monitoring console collects some data from...
9
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.