Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 14th, 2006, 6:46 PM   #1
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
2 dimension array of characters

Hi , I am stuck with two dimension array of characters.

for example, I have 4 lines in a file( lines should be treated as arrays of string) called source:

i am not good at C.
i hope improve soon.
i need help.
it is very cold.


I want read the first two lines and store them in a 2-dimension array of characters called destination.

I know how to read (all the lines but not the first two) but when I tried to input the line in destination I get errors: I tried strcpy, memcpy...

What can I do please?
B
while( !feof(fin) ) {/* while begins*/
           count=0;
           for(i=0;i<5;i++)
           {fgets(source,sizeof(source),fin);
           strcpy(destination,source);
             count++
      /*if(count%2==0)*/
      fputs(hand,fout);
does not give what I want!!
brad sue is offline   Reply With Quote
Old Mar 14th, 2006, 7:02 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
What's wrong with using your count? Just count to two, don't look for the remainder unless you have some other objective.

Be very careful using eof as a loop exit at the top of the loop. Behavior can vary depending upon whether or not the last line in the file ends with a newline. Either way you decide to go can screw up performance if the other condition occurs. You'll wind up losing the last line or using it twice. I recommend making a "while (1)" loop and exiting explicitly when you detect EOF or error. Refer to your 'fgets' documentation very carefully.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 15th, 2006, 3:19 AM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Like DaWei said, just count to two. Here's what you can do.
[php]#include <stdio.h>
#include <string.h>

uhOh(char * error)
{
fprintf(stderr, "%s\n", error);
return 1;
}

main()
{
FILE * fin;
int i, count = 2;
char source[256];
char destination[2][256];

if((fin = fopen("source.txt", "rb")) == NULL)
{
return uhOh("Cannot open file");
}

for(i = 0; i < count; i++)
{
if(fgets(source, 100, fin) == NULL)
{
if(ferror(fin))
return uhOh("An error has occured, check it with ferror()");
if(feof(fin))
{
printf("done\n");
return 0;
}
}
else
{
strcpy(destination[i],source);
printf("LINE: %s\n", destination[i]);
}
}
fclose(fin);
}[/php]

You don't get to printing "done" because the for loop ends, but try filling in count = 5 instead.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 15th, 2006, 7:03 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
ROFL, Ruben! You KNOW that I have the uhOh function and close relatives copyrighted!!!111!!eleven!
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 15th, 2006, 9:01 AM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by DaWei
ROFL, Ruben! You KNOW that I have the uhOh function and close relatives copyrighted!!!111!!eleven!
Haha, I always see you using it, so I thought I'd learn from the pro's.

@brad sue: The uhOh function with courtesy of DaWei.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 15th, 2006, 9:18 AM   #6
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
[php]
return uhOh("An error has occured, check it with ferror()");[/php]
should be:
[php]
return uhOh("Trouble in River City.");
[/php]
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Mar 15th, 2006, 9:30 AM   #7
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
Quote:
Originally Posted by InfoGeek
[php]
return uhOh("An error has occured, check it with ferror()");[/php]
should be:
[php]
return uhOh("Trouble in River City.");
[/php]
Infogeek,
Thank you very much
I am writing a standard C code, not php, but I think the logic is the same..
I will try it now.
Thank you
B
brad sue is offline   Reply With Quote
Old Mar 15th, 2006, 9:36 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The 'PHP' tags don't necessarily indicate PHP code, bradsue, they cause syntax highlighting to occur whereas the ordinary 'code' tags don't. InfoGeek is teasing me because I often name the string in the function prototype as he has shown .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 15th, 2006, 9:40 AM   #9
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
Rep Power: 3 brad sue is on a distinguished road
OOHH!
I am sorry ( I know so littlle!!)

B.
brad sue is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC