Storing in array

K

Kay

ABC
ASF
DFS
ASS
ABC <--- Duplicate
JJK

I want to store above char in an arry, but I'm not sure I'm right or not
anyone can guide me or give me some suggestions, Thx?

for( int i = 0; i < 7; i++)
array = "\n";

While( !feof(ptr)){

fscanf(fptr, "%s", port );

for(int i = 0; i < 7; i++ ){

if( array != port && array == "\n" ){
strcpy( array, port );
}
}
 
K

Karthik Kumar

Kay said:
ABC
ASF
DFS
ASS
ABC <--- Duplicate
JJK

I want to store above char in an arry, but I'm not sure I'm right or not
anyone can guide me or give me some suggestions, Thx?

Where is the declaration of the variable 'array' ?
As a matter of style, try to give it a meaningful name
other than array . There could be too many arrays in your
program.
for( int i = 0; i < 7; i++)

How do you know this magic number 7 beforehand ?
Are you making an assumption that the input
file is always going to contain at least 7 lines ?

array = "\n";


Why ? Even if you had meant to assign '\n' character
to all the contents it is still wrong.
You have to use strcpy.
While( !feof(ptr)){

You had posted a code fragment here.
You have to open a file, in the appropriate mode
before you could do this.
fscanf(fptr, "%s", port );

for(int i = 0; i < 7; i++ ){

if( array != port && array == "\n" ){
strcpy( array, port );
}
}


Check out strcmp if you want to compare two strings.
Please post fully compilable code to get better help.
 
J

Jens.Toerring

Kay said:
ABC
ASF
DFS
ASS
ABC <--- Duplicate
JJK
I want to store above char in an arry, but I'm not sure I'm right or not
anyone can guide me or give me some suggestions, Thx?

All of this is horribly broken. You use 'array' in some places as if
it would be an array of char pointers and in others as if it would be
an array of char arrays - at least that's what it looks like if you
first assign a pointer to an element of array and later use strcpy()
to copy to one or more of the elements. Since there's no definition
of 'array' it's impossible to figure out what you meant it to be.
for( int i = 0; i < 7; i++)
array = "\n";


Here you assign a pointer to the string literal "\n" to the first 7
elements of 'array' which thus must be an array of (at least 7) char
pointers. Or did you meant to do a strcpy() here?
While( !feof(ptr)){

First, "While" must start with a lower case letter. Second, feof()
can only be used after you already have tried to read from the
file, typically you use it to find out if a read failed because
you hit the end of the file. It won't tell you if the next read
is going to fail because of that condition.
fscanf(fptr, "%s", port );

This is rather dangerous unless you know exactly that there's
never going to be a longer that's longer than the memory assigned
to 'port. Note that you can pass a maximum length of the string
to be read with '%s' top fscanf() - that can help you eliminate
this potential problem.
for(int i = 0; i < 7; i++ ){

if( array != port && array == "\n" ){


Now, why should 'port' and array ever be identical? I guess you
try to do a string comparison here but that's not how that works.
What you do here is comparing the pointers array and 'port'
but not the strings they are pointing to. There's strcmp() for
that.
strcpy( array, port );


Since all elements of array have proviously been set to point to
the literal string "\n" this strcpy() won't work - you try to
copy what 'port' points to over that literal string. But, first
of all, literal strings can't be changed, and second, even if
you could do that it would also change what all the other elements
of 'array' point to.

It looks a bit as if you have to try to figure out more clearly
for yourself what the difference between an array and a pointer
is and that they aren't the same. The next time please post a
complete program (or at least a complete function) because with-
out knowing how you defined your variables it's impossible to
say if you got it right or not.

Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top