473,379 Members | 1,245 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,379 software developers and data experts.

Storing in array

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[i] = "\n";

While( !feof(ptr)){

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

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

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

Nov 14 '05 #1
3 1793
Kay wrote:
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[i] = "\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[i] != port && array[i] == "\n" ){
strcpy( array[i], port );
}
}


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

--
Karthik.
http://akktech.blogspot.com .
Nov 14 '05 #2
Karthik Kumar wrote:
Kay wrote:
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++)


Try to use size_t for array indices instead of
int. It fits better.
array[i] = "\n";


--
Karthik.
http://akktech.blogspot.com .
Nov 14 '05 #3
Kay <er*********@yahoo.com.hk> wrote:
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[i] = "\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[i] != port && array[i] == "\n" ){
Now, why should 'port' and array[i] 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[i] and 'port'
but not the strings they are pointing to. There's strcmp() for
that.
strcpy( array[i], 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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Tor Erik Sønvisen | last post by:
Hi I need a time and space efficient way of storing up to 6 million bits. Time efficency is more important then space efficency as I'm going to do searches through the bit-set. regards tores
3
by: Brad | last post by:
I am storing an array which contains about a dozen chracter items to a Session variable. Later, I need to use this array so I am doing the following: Dim eventTypes As String() =...
7
by: paladin.rithe | last post by:
Is it possible to store classes in an array? I am fairly new to PHP, and haven't found anything either way yet. I have a program that where you can have multiple notes attached to a ticket, which...
22
by: guitarromantic | last post by:
Hey everyone, I run a site with staff-submitted reviews, and most of them are written by one author. However, we also do "multiple" reviews. Up until now I just had a userid for a 'Multiple'...
2
by: gh | last post by:
Hi, I have a string variable which contains n number of comma delimited elements and I would like to store each element into an array but I could not figure how to do it. for example,...
6
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
3
by: ArmsTom | last post by:
I was using structures to store information read from a file. That was working fine for me, but then I read that anything stored in a structure is added to the stack and not the heap. So, I made...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
1
by: Miesha.James | last post by:
Hello, I'm trying to rewrite visual c++ code into visual c++ .NET code and I've run across a problem with storing objects into a list. Here;s an example of the code I have: ref struct...
10
by: deciacco | last post by:
I'm writing a command line utility to move some files. I'm dealing with thousands of files and I was wondering if anyone had any suggestions. This is what I have currently: $arrayVirtualFile =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.