A
arnuld
/* A C program that reads a file and copies the contents to a new file
while discarding all the repeated words.
* Written by one of my friends, posted by me on CLC for constructive
criticism. I dont' think its a standard
* C program, hence I posted it here to make it one
*
* VERSION 0.0
*
*/
#define __GNU__SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char str1[50] = {0};
char *array[100];
FILE *ifp,*ofp;
FILE *baseifp;
int i = 0,k=0,flag;
ifp = fopen("myfile", "r");
if(ifp==NULL)
perror("input File is not open");
ofp = fopen("outputfile", "w");
if(ofp==NULL)
perror("output File is not open");
char * line = NULL;
size_t len = 0;
ssize_t read;
/* print read elements on stdout */
while ((read = getline(&line, &len, ifp)) != -1) {
printf("Retrieved line of length %zu :\n", read);
printf("%s", line);
}
if (line)
free(line);
//fclose (ifp);
ifp = freopen("myfile", "r", ifp);
while(fscanf(ifp, "%s", str1)!=EOF)
{
printf ("%s\n",str1);
flag = 0;
array = (char *)malloc (strlen (str1)+1);
strcpy(array,str1);
if(i > 0)
for (k = 0; k < i ; k++)
{
if (strcmp(array[k], str1)==0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
fprintf(ofp, "%s ", str1);
}
i++;
memset (str1, 0, 50);
}
printf ("\n");
fclose(ifp);
fclose(ofp);
return 0;
}
================== OUTPUT ========================
[arnuld@dune programs]$ gcc -std=c99 -pedantic -Wall -Wextra remove-
repeated-words.c
remove-repeated-words.c: In function ‘main’:
remove-repeated-words.c:33: error: ‘ssize_t’ undeclared (first use in
this function)
remove-repeated-words.c:33: error: (Each undeclared identifier is
reported only once
remove-repeated-words.c:33: error: for each function it appears in.)
remove-repeated-words.c:33: error: expected ‘;’ before ‘read’
remove-repeated-words.c:35: error: ‘read’ undeclared (first use in this
function)
remove-repeated-words.c:35: warning: implicit declaration of function
‘getline’
remove-repeated-words.c:19: warning: unused variable ‘baseifp’
[arnuld@dune programs]$
Everything is explained in the comments, I have these ideas:
1) First #define __GNU_SOURCE has to go, its not a standard C facility.
2) getline() is not a C function, so I think using fgets() will be a
better idea ?
Will post the code as soon as I rewrite it. Till then can I have your
views ?
while discarding all the repeated words.
* Written by one of my friends, posted by me on CLC for constructive
criticism. I dont' think its a standard
* C program, hence I posted it here to make it one
*
* VERSION 0.0
*
*/
#define __GNU__SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char str1[50] = {0};
char *array[100];
FILE *ifp,*ofp;
FILE *baseifp;
int i = 0,k=0,flag;
ifp = fopen("myfile", "r");
if(ifp==NULL)
perror("input File is not open");
ofp = fopen("outputfile", "w");
if(ofp==NULL)
perror("output File is not open");
char * line = NULL;
size_t len = 0;
ssize_t read;
/* print read elements on stdout */
while ((read = getline(&line, &len, ifp)) != -1) {
printf("Retrieved line of length %zu :\n", read);
printf("%s", line);
}
if (line)
free(line);
//fclose (ifp);
ifp = freopen("myfile", "r", ifp);
while(fscanf(ifp, "%s", str1)!=EOF)
{
printf ("%s\n",str1);
flag = 0;
array = (char *)malloc (strlen (str1)+1);
strcpy(array,str1);
if(i > 0)
for (k = 0; k < i ; k++)
{
if (strcmp(array[k], str1)==0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
fprintf(ofp, "%s ", str1);
}
i++;
memset (str1, 0, 50);
}
printf ("\n");
fclose(ifp);
fclose(ofp);
return 0;
}
================== OUTPUT ========================
[arnuld@dune programs]$ gcc -std=c99 -pedantic -Wall -Wextra remove-
repeated-words.c
remove-repeated-words.c: In function ‘main’:
remove-repeated-words.c:33: error: ‘ssize_t’ undeclared (first use in
this function)
remove-repeated-words.c:33: error: (Each undeclared identifier is
reported only once
remove-repeated-words.c:33: error: for each function it appears in.)
remove-repeated-words.c:33: error: expected ‘;’ before ‘read’
remove-repeated-words.c:35: error: ‘read’ undeclared (first use in this
function)
remove-repeated-words.c:35: warning: implicit declaration of function
‘getline’
remove-repeated-words.c:19: warning: unused variable ‘baseifp’
[arnuld@dune programs]$
Everything is explained in the comments, I have these ideas:
1) First #define __GNU_SOURCE has to go, its not a standard C facility.
2) getline() is not a C function, so I think using fgets() will be a
better idea ?
Will post the code as soon as I rewrite it. Till then can I have your
views ?