rohit wrote:
Hi All,
I am new to C language.I want to read integers from a text file and
want to do some operation in the main program.To be more specific I
need to multiply each of these integers with another set of integers
stored in an array.It would be a great help if you could provide some
code for it.I tried the function fscanf but by that I am able to read
only the first integer of the text file.Please help me.
If you have any questions about the code,
I'll answer them as best as I can.
/* BEGIN new.c output */
File is open for writing.
File values:
150, 140, 242, 72, 14
File is closed.
Array values:
3, 1, 3, 1, 3
File is open for reading.
Reading file into a linked list...
File is closed.
File is open for writing.
Overwriting file with product of list and array values...
List is freed.
File is closed.
File is open for reading.
File values:
450, 140, 726, 72, 42
Line reading buffer is freed.
File is closed.
File is removed.
/* END new.c output */
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define N 5
#define NMEMB(A) (sizeof (A) / sizeof *(A))
#define LU_RAND_SEED 123456789LU
#define LU_RAND(S) ((S) * 69069LU + 362437 & 0XFFFFFFFF)
struct list_node {
struct list_node *next;
void *data;
};
typedef struct list_node list_type;
int get_line(char **lineptr, size_t *n, FILE *stream);
list_type *list_append
(list_type **head, list_type *tail, void *data, size_t size);
void list_free(list_ type *node, void (*free_data)(vo id *));
int main(void)
{
int rc;
size_t index;
FILE *fp;
char fn[L_tmpnam];
long unsigned array[N];
long unsigned seed = LU_RAND_SEED;
char *buff = NULL;
size_t size = 0;
list_type *head = NULL;
list_type *tail = NULL;
puts("/* BEGIN new.c output */\n");
tmpnam(fn);
fp = fopen(fn, "w");
if (fp == NULL) {
fputs("fopen(fn ), \"w\") == NULL\n", stderr);
exit(EXIT_FAILU RE);
}
puts("File is open for writing.");
puts("File values:");
for (index = 0; index != NMEMB(array); ++index) {
seed = LU_RAND(seed) & 0xff;
fprintf( fp, "%lu\n", seed);
fprintf(stdout, "%4lu,", seed);
seed = LU_RAND(seed);
array[index] = seed & 0x3;
}
puts("\b ");
fclose(fp);
puts("File is closed.");
puts("Array values:");
for (index = 0; index != NMEMB(array); ++index) {
fprintf(stdout, "%4lu,", array[index]);
}
puts("\b ");
fp = fopen(fn, "r");
if (fp == NULL) {
fputs("fopen(fn , \"r\") == NULL\n", stderr);
exit(EXIT_FAILU RE);
}
puts("File is open for reading.");
puts("Reading file into a linked list...");
while ((rc = get_line(&buff, &size, fp)) 0) {
tail = list_append(&he ad, tail, buff, rc);
if (tail == NULL) {
fputs("tail == NULL\n", stderr);
break;
}
}
fclose(fp);
puts("File is closed.");
fp = fopen(fn, "w");
if (fp == NULL) {
fputs("fopen(fn ), \"w\") == NULL\n", stderr);
exit(EXIT_FAILU RE);
}
puts("File is open for writing.");
puts("Overwriti ng file with product of list and array values...");
for (tail = head, index = 0; index != NMEMB(array); ++index) {
seed = strtoul(tail -data, NULL, 10);
fprintf(fp, "%lu\n", array[index] * seed);
tail = tail -next;
if (tail == NULL) {
break;
}
}
list_free(head, free);
puts("List is freed.");
fclose(fp);
puts("File is closed.");
fp = fopen(fn, "r");
if (fp == NULL) {
fputs("fopen(fn , \"r\") == NULL\n", stderr);
exit(EXIT_FAILU RE);
}
puts("File is open for reading.");
puts("File values:");
while ((rc = get_line(&buff, &size, fp)) 0) {
fprintf(stdout, "%4s,", buff);
}
puts("\b ");
free(buff);
buff = NULL;
size = 0;
puts("Line reading buffer is freed.");
fclose(fp);
puts("File is closed.");
remove(fn);
puts("File is removed.");
puts("\n/* END new.c output */");
return 0;
}
int get_line(char **lineptr, size_t *n, FILE *stream)
{
int rc;
void *p;
size_t count;
count = 0;
while ((rc = getc(stream)) != EOF
|| !feof(stream) && !ferror(stream) )
{
++count;
if (count == (size_t)-2) {
if (rc != '\n') {
(*lineptr)[count] = '\0';
(*lineptr)[count - 1] = (char)rc;
} else {
(*lineptr)[count - 1] = '\0';
}
break;
}
if (count + 2 *n) {
p = realloc(*linept r, count + 2);
if (p == NULL) {
if (*n count) {
if (rc != '\n') {
(*lineptr)[count] = '\0';
(*lineptr)[count - 1] = (char)rc;
} else {
(*lineptr)[count - 1] = '\0';
}
} else {
if (*n != 0) {
**lineptr = '\0';
}
ungetc(rc, stream);
}
count = 0;
break;
}
*lineptr = p;
*n = count + 2;
}
if (rc != '\n') {
(*lineptr)[count - 1] = (char)rc;
} else {
(*lineptr)[count - 1] = '\0';
break;
}
}
if (rc != EOF || !feof(stream) && !ferror(stream) ) {
rc = INT_MAX count ? count : INT_MAX;
} else {
if (*n count) {
(*lineptr)[count] = '\0';
}
}
return rc;
}
list_type *list_append
(list_type **head, list_type *tail, void *data, size_t size)
{
list_type *node;
node = malloc(sizeof *node);
if (node != NULL) {
node -next = NULL;
node -data = malloc(size);
if (node -data != NULL) {
memcpy(node -data, data, size);
if (*head != NULL) {
tail -next = node;
} else {
*head = node;
}
} else {
free(node);
node = NULL;
}
}
return node;
}
void list_free(list_ type *node, void (*free_data)(vo id *))
{
list_type *next_node;
while (node != NULL) {
next_node = node -next;
free_data(node -data);
free(node);
node = next_node;
}
}
/* END new.c */
--
pete