473,796 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1816
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*********@ya hoo.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***********@p hysik.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
2341
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
2338
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() = DirectCast(Session("EventTypes"), String()) If Date.Today <= closeDate Then If eventTypes(cblEntries.SelectedIndex) = "J" Then thisFee = Session("JRFee") Else thisFee = Session("PEFee") Else If eventTypes(cblEntries.SelectedIndex) = "J" Then thisFee =
7
1545
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 are stored in a database. I would like to just pull all the notes from the database, storing each one in a seperate class, which itself is stored in an array (well, another class, but it's a classList, it's mainly an array). I'm getting an...
22
2560
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' account and submitted them under that, but this makes it harder to print lists of all the reviews by one person, so ideally I wanna make a multiple select form so we can just select all the contributors and have the values saved in the database - in...
2
2548
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, 1stitem,2nditem,3rditem,4thitem, ..., nthitem What would be the best way of storing the above string into an array like
6
3186
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 entries with the entry in the other table? Having a separate table would result in two queries instead of one, but you wouldn't have to deal with the overhead of serializing and unserializing data. -- Kyle
3
1554
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 a class that stores the same information. The user selects any number of records from the file when the program loads & can then make changes. The records the user selects are added to an array and changes are made to the records in that array...
20
4652
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
2179
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 Cookies { String^ Name;
10
2314
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 = array( 'filename'=>'filename', 'basename'=>'filename.ext', 'extension'=>'ext', 'size'=>0,
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.