Hi All,
Assume there are some data in "a.txt":
e.g.
ABC
DEF<--------duplicate
GHI
DEF<--------duplicate
JKL
MNO
PQR
STU<--------duplicate
STU<--------duplicate
STU<--------duplicate
VWX
YZA
XYZ
XYZ
CAD
YZA
KLS
GAE
PQR
GAE
ABC
SAC
MTF
I would like to reduce the duplication of data so that to form:
e.g.
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
YZA
XYZ
CAD
KLS
GAE
SAC
MTF
Here are my codes, but seems they aren't work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct H {
char name[5];
int l;
};
H r[50];
int main() {
char a[5];
int count =0;
FILE *fin = fopen("a.txt", "r");
for(i=0; !feof(fin); i++) {
//Set as Null
strcpy(r[i].name, "N");
fscanf(f, "%s", a);
for(i=0; i<50 ; i++) {
// Check for null
if(strcmp(r[i].name, "N") == 0) {
strcpy(r[i].name, a);
i=50;
break;
} else {
for(i=0; i<50 ; i++) {
if(strcmp(r.name, a) == 0) {
count++;
i=50+i;
} else {
for(i=0; i<50 ; i++) {
if(strcmp(r[i+1].name, a) == 0) {
count++;
i=50+i;
} else {
i++;
strcpy(r[i+count].name, a);
}
}
}
}
}
}
}
How should I improve those so that to generate data with no duplication?
Please help.
THANKS ALL! 3 2036
Tcc <as**@yahoo.com> wrote: Assume there are some data in "a.txt": e.g.
[snip]
#include <stdio.h> #include <stdlib.h> #include <string.h>
struct H { char name[5]; int l; };
H r[50];
struct H r[50];
int main() { char a[5]; int count =0; FILE *fin = fopen("a.txt", "r");
for(i=0; !feof(fin); i++) {
You didn't declare `i'. Did this compile?
feof() will tell you the truth after read attempt, not before (see below).
//Set as Null strcpy(r[i].name, "N"); fscanf(f, "%s", a);
Here you might check:
if (feof(fin))
break; for(i=0; i<50 ; i++) {
Here (and below) you use `i' again for indexing. This will overwrite
the `i' from the outer loop. I don't think you want this, you have
to use another variable.
// Check for null if(strcmp(r[i].name, "N") == 0) {
Here you check if you've reached current element in the output table.
Will work until a.txt actually contains the word "N". strcpy(r[i].name, a); i=50; break;
If you break from the current loop, you don't have to set the index
to the end value.
The rest is just plain nonsense. } else { for(i=0; i<50 ; i++) { if(strcmp(r.name, a) == 0) { count++; i=50+i; } else { for(i=0; i<50 ; i++) { if(strcmp(r[i+1].name, a) == 0) { count++; i=50+i; } else { i++; strcpy(r[i+count].name, a); } } } } } } }
How should I improve those so that to generate data with no duplication?
Rethink, redesign, start all over. Best on paper first.
What you need is (pseudo-code):
#define MAX 50
char words[MAX][5]
count=0
while read(word) && count < MAX
/* check if we have it */
found = false
for i=0; i<count; ++i
if word == words[i]
found = true
break; //not inevitable, makes program faster
/* if word is new, add it */
if !found
words[count++] = word
--
Stan Tobias
sed 's/[A-Z]//g' to email
"Tcc" <as**@yahoo.com> writes: I would like to reduce the duplication of data so that to form:
If you're trying to learn how to write C code, be my guest.
However, you should be aware that there are lots of existing
tools to do what you want already. For example, under Unix the
command
sort < x | uniq > y
is probably what you want, although it has the extra side effect
of sorting the output.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
"Tcc" <as**@yahoo.com> wrote in message news:ck**********@news.hgc.com.hk... Hi All,
I would like to reduce the duplication of data so that to form:
It is better to do it with Perl, AWK, from within vi editor by substitution,
or from UNIX/LINUX command line (sort first) etc.
In C, one way (little bit clumsy, but works) is this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct lNode
{
char val[5];
struct lNode *next;
} lNode;
lNode *append (lNode *p, char val[5])
{
lNode *last = p;
while (p)
{
last = p;
if (!strcmp(p->val,val))
return NULL;
p = p->next;
}
if (last)
{
last->next = (lNode *)malloc(sizeof(lNode));
strcpy(last->next->val,val);
last->next->next = NULL;
last = last->next;
}
else
{
// First node
last = (lNode *)malloc(sizeof(lNode));
strcpy(last->val,val);
last->next = NULL;
}
return last;
}
void list_print(lNode *list)
{
lNode *node;
node = list;
while (node)
{
printf("%s",node->val);
node = node->next;
}
printf("\n");
}
int main (void)
{
lNode *head = NULL;
lNode *node, *p;
FILE *fin = fopen("a.txt", "r");
FILE *fout;
char line[5];
int i;
// Make a linked list with words
for (i = 0; (i < 50)&&!feof(fin); i++)
{
if(fgets(line, 5, fin) != NULL)
node = append (head, line);
if (i == 0) head = node;
}
// Output the result
printf("The list:\n");
list_print(head);
fclose(fin);
// Put it back to txt file
fout = fopen("a.txt", "w");
p = head;
while(p != NULL)
{
fputs(p->val,fin);
p = p->next;
}
fclose(fout);
return 0;
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Stephen C. Waterbury |
last post by:
This seems like it ought to work, according to the
description of reduce(), but it doesn't. Is this
a bug, or am I missing something?
Python 2.3.2 (#1, Oct 20 2003, 01:04:35)
on linux2
Type...
|
by: Tom Anderson |
last post by:
Comrades,
During our current discussion of the fate of functional constructs in
python, someone brought up Guido's bull on the matter:
http://www.artima.com/weblogs/viewpost.jsp?thread=98196
...
|
by: Martin Andersson |
last post by:
Hello
I have an xml structure (or planing to have) something that will have a
structure like this.
<event name="XXXX">
<param name="a"\>
<param name="b"\>
<param name="xxxx"\>
</event>
|
by: Steve Jorgensen |
last post by:
A while back, I started boning up on Software Engineering best practices and
learning about Agile programming. In the process, I've become much more
committed to removing duplication in code at a...
|
by: patelxxx |
last post by:
Currently when I select one of the radio buttons and then press submit, the answer which is alerted is always 'false', even if i press 'true', I still get 'false' alerted. What I'm i doing wrong?...
|
by: =?Utf-8?B?S2F2aXRh?= |
last post by:
We have one web application developed in .Net framework 1.1. This site is
live on production server since one year and used across world by many users.
But only one of the users is facing...
|
by: dbarmer |
last post by:
I have a unix database system that hold three tables, 254, 255, 256, and all are linked together by WO, CUST NO, LOC NO. This is for Unix Program Purposes Only. Duplication here is by design. So...
|
by: arnuld |
last post by:
I have created a program which creates and renames files. I have
described everything in comments. All I have is the
cod-duplication. function like fopen, sprint and fwrite are being called
again...
|
by: henry |
last post by:
Folks:
Using Dreamweaver CS3...
Consider a home page, "index.php" which conditionally REQUIREs one of 'N'
HTML files of pure content. All site styles are specified in a master CSS
file,...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
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,...
| |