A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 3 years ago
c prog prob
anonymous
 3 years ago
c prog prob

This Question is Closed

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0here's the task....it has to read the first line of the file and copy every word into a 2d array but for some reason.....the array is empty.....

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0i tried two methods initially find the no.of character's in the file in the line 1 and create a dynamic array and copy the contents in to it.......(linear_array) then method 1: copy every char in to array ...if a space is encountered increment row pointer of 2d array method 2 : replace every space in the linear_array and replace it with \0 then copy each string into each row both of them dint work the array is either empty or copies a single word here's the test file used gcc

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0You replace every space with '\0', Suppose the file contains "Hello, World". In your loop, you ask for strlen(p), which will be 6 on the first iteration (matching "Hello,"). This one is copied just fine. The second iteration however, p points to '\0' (what used to be the space). So strlen(p) is 0 the second iteration (and all other iterations). So strlen does NOT include the '\0'. Btw, don't forget to free the memory you malloced

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0no i need it later so i cant free it yet.......that's not the entire prog thanks....but y dint the other method work like copying the array char by char?

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0the one i commented out

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0i think i got ur xplanation but when i used strlen(p) i got a result of 5 like strlen("\0askldghal"\0") should give a zero?

anonymous
 3 years ago
Best ResponseYou've already chosen the best response.0The problem with the commented code is the the elsepart of the forloop. You first increment k before assigning anything. Because of this, array[j][0] (the start of the string) will remain 0. strlen("\0askl...") will indeed give 0 length. You could also use the strtok function from the standard library to split a string into words (see: http://www.cplusplus.com/reference/cstring/strtok/ ).
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.