d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > 500 Forum Gold - Writing Program C > -
Add Reply New Topic New Poll
Member
Posts: 8,732
Joined: Nov 21 2007
Gold: 775.00
Oct 14 2013 07:41pm
Writing assignment for my class, I understand the portions completely, and I understand the body of the code that is given here.

I need someone to write the entire program and make sure it works. Paying up to 500 Forum Gold for this. It's very basic. This is a second tier intro course to C.

It is due Wednesday 10/16 at 11:59 Pm (midnight)



It's basically reading strings from a FILE, and publishing them to an output file, without using scanf. And properly allocating space to do so.

Using pointer in/pointer outs/malloc/free ONLY.

Very basic.






I have the PDF link, if you're interested in this PM me for it. It's a very detailed assignment cover.
Member
Posts: 8,732
Joined: Nov 21 2007
Gold: 775.00
Oct 15 2013 05:06pm
I have written most of it, this is the part I need written

Basically the program uses a pointer to read in a string of unknown length, I have to determine the length of the string that is read-in by using the function below, to allocate space and create a string (array) out of it so I can send it to an out file pointer.
Not allowed to use strlen function of course.



#define DEFLEN 1 /* 1 is good number for testing */
#define CHUNSZ 1 /* 1 is good number for testing */
/*
* This function safely reads a complete line of unknown length from the
* open file pointed to by fpin. It returns a line that is at most CHUNKSZ-1
* characters longer than the minimum needed to hold the line.
* It initially allocates an array of DEFLEN characters to hold the string,
* and if this space is inadequate to hold the string, it will iteratively
* create a new string that is CHUNKSZ larger, copy the old string to it
* release the old string, and then read in more characters from the file,
* and continue this until the entire line of arbitrary length can be returned.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 15 2013 05:15pm
iirc a string terminates with a null. so just keep reading until you hit null.
Member
Posts: 8,732
Joined: Nov 21 2007
Gold: 775.00
Oct 15 2013 05:29pm
Quote (carteblanche @ Oct 15 2013 05:15pm)
iirc a string terminates with a null. so just keep reading until you hit null.


I'm so new to programming

So...

char *ln;
ln = getStrFromFile(fpin);

while ( ln != NULL)
{
...
}


char getStringFromFile(FILE *fpin)
{
char *array[256]

fgets(array, 256, fpin);
return array;
}


It's imperative that I use the function provided, it's more for learning and the professor wants to see it to give credit for it. Basically increasing a string by 1 and copying it until I have sufficient length.
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Oct 18 2013 02:40pm
Quote
char getStringFromFile(FILE *fpin)
{
char *array[256]

fgets(array, 256, fpin);
return array;
}


Should be "char* getStringFromFile(FILE *fpin)"

I'd advise against char *array[256]. use char *array = (char*)malloc(256); instead

Code
int l=0; //this will have your string length
while(ln[l] && l<256) l++;

//do shit with ln

free(ln);


that's your basic way to grab the null terminator, without bursting out of the predefined length

getStringFromFile is a little weak though. fgets won't read the full buffer size if it hits a null character. You're better off feeding the pointer to it as an argument and using it to return the amount of characters read, which will implicitly give you the string length. Also you should consider zeroing the buffer before reading to it, better practice when it comes to random size strings.

This post was edited by flyinggoat on Oct 18 2013 02:41pm
Member
Posts: 17,177
Joined: Aug 10 2007
Gold: 125.00
Dec 19 2013 07:31pm
What about using sizeof?
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Dec 19 2013 07:39pm
Way to necro.

sizeof won't work - it'll either return the size of the buffer array or the size of a pointer, neither of which is what you want.

Here's an example: http://ideone.com/gPfBJI
Member
Posts: 17,177
Joined: Aug 10 2007
Gold: 125.00
Dec 20 2013 03:16am
Oh someone else necro'd before me. But you can size of a pointer. I had to do it in a project before.

Code
int ARRAY_SIZE((sizeof pstr)/(sizeof pstr[0]));

char* pstr[]={ "a", "b", "c", "d" }

for(int i=0; i<ARRAY_SIZE; i++){
cout << *(pstr+i) << " ";
}


Here's part of it. I don't want to post the whole thing because the programs big. But this is the general idea. There are multiple ways to do this.
Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Dec 20 2013 04:54am
Quote (Ayudame @ 20 Dec 2013 11:16)
Oh someone else necro'd before me. But you can size of a pointer. I had to do it in a project before.


No? 18 Oct to 20 Dec, you necromancer.
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Dec 20 2013 05:19am
Quote (Ayudame @ 20 Dec 2013 10:16)
Oh someone else necro'd before me. But you can size of a pointer. I had to do it in a project before.

Code
int ARRAY_SIZE((sizeof pstr)/(sizeof pstr[0]));

char* pstr[]={ "a", "b", "c", "d" }

for(int i=0; i<ARRAY_SIZE; i++){
  cout << *(pstr+i) << " ";
  }


Here's part of it. I don't want to post the whole thing because the programs big. But this is the general idea. There are multiple ways to do this.

sizeof of a pointer will yield size of the pointer, not size of the value under it. sizeof of an array will yield the size of the array. Neither of which are suitable to tell length of a string: it may be shorter than the full length of the buffer and it's not very likely to match size of a pointer. All in all, even if it is occasionally right, it's a case of a broken clock being right twice a day.

In your example you use a classic array size macro idiom, but it won't work if size of your array size doesn't match the length of a string, or if there is no array to begin with - that is when you're dealing with a pointer. And I have already posted an example displaying both these cases giving incorrect results: http://ideone.com/gPfBJI
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll