473,402 Members | 2,055 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,402 software developers and data experts.

I have no idea how to attack this problem

say my input file is

[cdalten@localhost ~]$ more suck
______
< gnu? >
------
\ , ,
\ /( )`
\ \ \___ / |
/- _ `-/ '
(/\/ \ \ /\
/ / | ` \
O O ) / |
`-^--'`< '
(_.) _ ) /
`.___/` /
`-----' /
<----. __ / __ \
<----|====O)))==) \) /====
<----' `--' `.__,' \
| |
\ /
______( (_ / \______
,' ,-----' | \
`--{__________) \/


[cdalten@localhost ~]$

What I want to do is read this file in, store it in some kind of array
and then be able to print this on stdout. I thought maybe i could do
something like the following

read in each line of the art via fgets()
store each line in an array
repeat

now print the array that holds these lines
Chad

Jul 18 '07 #1
5 1704
"Chad" <cd*****@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
say my input file is

[cdalten@localhost ~]$ more suck
______
< gnu? >
------
\ , ,
\ /( )`
\ \ \___ / |
/- _ `-/ '
(/\/ \ \ /\
/ / | ` \
O O ) / |
`-^--'`< '
(_.) _ ) /
`.___/` /
`-----' /
<----. __ / __ \
<----|====O)))==) \) /====
<----' `--' `.__,' \
| |
\ /
______( (_ / \______
,' ,-----' | \
`--{__________) \/


[cdalten@localhost ~]$

What I want to do is read this file in, store it in some kind of array
and then be able to print this on stdout. I thought maybe i could do
something like the following

read in each line of the art via fgets()
store each line in an array
repeat

now print the array that holds these lines
You don't even have to store them in an array. Just read a line, print a
line.

You didn't mention your langauge, it sounds like C but could be C++. Here
would be the C++ code, although it's not tested and may have bugs:

#include <ifstream>
#include <string>

int main()
{
std::ifsteam Input("suck");
std::string Line;
while ( std::getline( Input, Line ) )
std::cout << Line << "\n";
}

For C replace ifstream with whatever fgets uses (file?). Replace
std::string line with a c-style array char Line[100]; Replace the while
statement with whatever fgets returns on failure. Replace the std::cout
into output to std out.
Jul 18 '07 #2

"Chad" <cd*****@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
say my input file is

[cdalten@localhost ~]$ more suck
______
< gnu? >
------
\ , ,
\ /( )`
\ \ \___ / |
/- _ `-/ '
(/\/ \ \ /\
/ / | ` \
O O ) / |
`-^--'`< '
(_.) _ ) /
`.___/` /
`-----' /
<----. __ / __ \
<----|====O)))==) \) /====
<----' `--' `.__,' \
| |
\ /
______( (_ / \______
,' ,-----' | \
`--{__________) \/


[cdalten@localhost ~]$

What I want to do is read this file in, store it in some kind of array
and then be able to print this on stdout. I thought maybe i could do
something like the following

read in each line of the art via fgets()
store each line in an array
repeat

now print the array that holds these lines
You've got a 2d array of chars that make up the image. However you proably
don't know either dimension at compile time.
So you've got two choices of data structure

char *raster; /* malloced block to hold ASCII art image */
int width;
int height;

raster[y*width+x] = '*'; /* put a star at x, y */

or

char **lines; /* malloced list of pointer to lines */
int Nlines;
int linelen; /* this one you may not need if you put nuls on line
ends instead */

/* how to build the structure */
lines = malloc(Nlines * sizeof(char *));
for(i=0;i<Nlines;i++)
lines[i] = malloc(linelen + 1);
lines[y][x] = '*'; /* put a star at x, y */

The second form has some advantages, for instance it is easier to expand or
shrink the image using realloc().
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jul 18 '07 #3
On Wednesday 18 Jul 2007 8:18 am in comp.lang.c Chad <cd*****@gmail.com>
wrote:
Message ID: <11**********************@o11g2000prd.googlegroups .com>
say my input file is

[cdalten@localhost ~]$ more suck
______
< gnu? >
------
\ , ,
\ /( )`
\ \ \___ / |
/- _ `-/ '
(/\/ \ \ /\
/ / | ` \
O O ) / |
`-^--'`< '
(_.) _ ) /
`.___/` /
`-----' /
<----. __ / __ \
<----|====O)))==) \) /====
<----' `--' `.__,' \
| |
\ /
______( (_ / \______
,' ,-----' | \
`--{__________) \/


[cdalten@localhost ~]$

What I want to do is read this file in, store it in some kind of array
and then be able to print this on stdout. I thought maybe i could do
something like the following

read in each line of the art via fgets()
store each line in an array
repeat

now print the array that holds these lines
It all depends on whether you want to read in the entire file into memory
before further processing or do it line by line, or even character by
character. You could also read in as an arbitrarily sized block, though
that's probably more common with non-text files.

To read in the entire file, I'd probably use getc in a loop with dynamic
buffer resizing, to accommodate more of the file, stopping when getc
returns EOF, for end-of-file or an error, or when memory allocation fails.

To read the file line by line, fgets is probably more appropriate for this
purpose. You might also consider using non-Standard but portable functions
like CBFalconer's ggets or Richard Heathfield's getline. In this scenario,
a dynamically allocated array of char * is probably most appropriate. Of
course, each of the char * must be further initialised to point to an array
of char to hold each line.

Your case is really no different from reading and writing any other text
file, regardless of the contents.

Jul 18 '07 #4
On Wednesday 18 Jul 2007 12:02 pm in comp.lang.c Malcolm McLean
<re*******@btinternet.comwrote:
Message ID: <B8*********************@bt.com>
>
"Chad" <cd*****@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
>say my input file is

[cdalten@localhost ~]$ more suck
______
< gnu? >
------
\ , ,
\ /( )`
\ \ \___ / |
/- _ `-/ '
(/\/ \ \ /\
/ / | ` \
O O ) / |
`-^--'`< '
(_.) _ ) /
`.___/` /
`-----' /
<----. __ / __ \
<----|====O)))==) \) /====
<----' `--' `.__,' \
| |
\ /
______( (_ / \______
,' ,-----' | \
`--{__________) \/


[cdalten@localhost ~]$

What I want to do is read this file in, store it in some kind of array
and then be able to print this on stdout. I thought maybe i could do
something like the following

read in each line of the art via fgets()
store each line in an array
repeat

now print the array that holds these lines
You've got a 2d array of chars that make up the image.
What image? It's a text file, composed of one or more lines.
However you proably
don't know either dimension at compile time.
So you've got two choices of data structure

char *raster; /* malloced block to hold ASCII art image */
int width;
int height;

raster[y*width+x] = '*'; /* put a star at x, y */
This is not an intuitive method for reading in a simple text file, though it
might be suitable for other purposes.
or

char **lines; /* malloced list of pointer to lines */
int Nlines;
int linelen; /* this one you may not need if you put nuls on
line ends instead */

/* how to build the structure */
lines = malloc(Nlines * sizeof(char *));
for(i=0;i<Nlines;i++)
lines[i] = malloc(linelen + 1);
lines[y][x] = '*'; /* put a star at x, y */
Why put a '*'?
The second form has some advantages, for instance it is easier to expand
or shrink the image using realloc().
I'd probably encapsulate the "inner" loop, which reads in the lines, into a
separate function. This function would return to the caller when no more
char * are available to assign to lines. The caller would then expand the
char * array and call the inner function again, and so on till end-of-file
or error.
Jul 18 '07 #5
Chad wrote:
>
say my input file is

[cdalten@localhost ~]$ more suck
______
< gnu? >
------
\ , ,
\ /( )`
\ \ \___ / |
/- _ `-/ '
(/\/ \ \ /\
/ / | ` \
O O ) / |
`-^--'`< '
(_.) _ ) /
`.___/` /
`-----' /
<----. __ / __ \
<----|====O)))==) \) /====
<----' `--' `.__,' \
| |
\ /
______( (_ / \______
,' ,-----' | \
`--{__________) \/

[cdalten@localhost ~]$

What I want to do is read this file in, store it in some kind of array
and then be able to print this on stdout. I thought maybe i could do
something like the following

read in each line of the art via fgets()
store each line in an array
repeat

now print the array that holds these lines
My preference for storing lines of a text file, is in a linked list.
I don't understand why you want to store the file contents
in memory prior to displaying it.
Without the storage, the whole program boils down to new.c

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
int c;
FILE *fp = fopen("file.txt", "r");

if (fp == NULL) {
puts("fp == NULL");
} else {
while ((c = getc(fp)) != EOF) {
putchar(c);
}
fclose(fp);
putchar('\n');
}
return 0;
}

/* END new.c */

type_1.c shows one way to read whole text files
into a linked list and display them.

/* BEGIN type_1.c */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

#define INITIAL_BUFFER_SIZE 0
#define ARGV_0 "type_1"

struct list_node {
struct list_node *next;
void *data;
};

int get_line(char **lineptr, size_t *n, FILE *stream);
void list_free(struct list_node *node, void (*free_data)(void *));
int list_fputs(struct list_node *node, FILE *stream);
struct list_node *string_node(struct list_node **head,
struct list_node *tail,
char *data);

int main(int argc, char *argv[])
{
int rc;
FILE *fd;
char *buff_ptr;
size_t buff_size;
struct list_node *head, *tail;

if (argc 1) {
buff_size = INITIAL_BUFFER_SIZE;
buff_ptr = malloc(buff_size);
if (buff_ptr == NULL && buff_size != 0) {
printf("malloc(%lu) == NULL\n", (long unsigned)buff_size);
exit(EXIT_FAILURE);
}
tail = head = NULL;
while (*++argv != NULL) {
fd = fopen(*argv, "r");
if (fd != NULL) {
while ((rc = get_line(&buff_ptr, &buff_size, fd))
0)
{
tail = string_node(&head, tail, buff_ptr);
if (tail == NULL) {
break;
}
}
fclose(fd);
switch (rc) {
case EOF:
if (buff_ptr != NULL
&& strlen(buff_ptr) != 0)
{
puts("rc equals EOF\n"
"The string in buff_ptr is:");
puts(buff_ptr);
tail = string_node(&head, tail, buff_ptr);
}
break;
case 0:
puts("realloc returned a null pointer "
"value in line_to_string.");
if (buff_size 1) {
puts("rc equals 0\n"
"The string in buff_ptr is:");
puts(buff_ptr);
tail = string_node(&head, tail, buff_ptr);
}
break;
default:
puts("malloc problem in string_node.");
break;
}
} else {
printf("\nfopen() problem with \"%s\"\n", *argv);
break;
}
list_fputs(head, stdout);
list_free(head, free);
head = NULL;
}
free(buff_ptr);
} else {
puts("Usage:\n>" ARGV_0
" <TEXT_FILE_0<TEXT_FILE_1<TEXT_FILE_2...\n");
}
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) {
if (count != -1) {
++count;
}
if (count + 2 *n) {
p = realloc(*lineptr, count + 2);
if (p == NULL) {
if (*n count) {
(*lineptr)[count] = '\0';
(*lineptr)[count - 1] = (char)rc;
} else {
if (*n != 0) {
**lineptr = '\0';
}
ungetc(rc, stream);
}
count = 0;
break;
}
*lineptr = p;
*n = count + 2;
}
if (rc == '\n') {
(*lineptr)[count - 1] = '\0';
break;
}
(*lineptr)[count - 1] = (char)rc;
}
if (rc != EOF) {
rc = count INT_MAX ? INT_MAX : count;
} else {
if (*n count) {
(*lineptr)[count] = '\0';
}
}
return rc;
}

void list_free(struct list_node *node, void (*free_data)(void *))
{
struct list_node *next_node;

while (node != NULL) {
next_node = node -next;
free_data(node -data);
free(node);
node = next_node;
}
}

int list_fputs(struct list_node *node, FILE *stream)
{
while (node != NULL) {
if (fputs(node -data, stream) == EOF) {
return EOF;
}
if (putc('\n', stream) == EOF) {
return EOF;
}
node = node -next;
}
return '\n';
}

struct list_node *string_node(struct list_node **head,
struct list_node *tail,
char *data)
{
struct list_node *node;

node = malloc(sizeof *node);
if (node != NULL) {
node -next = NULL;
node -data = malloc(strlen(data) + 1);
if (node -data != NULL) {
strcpy(node -data, data);
if (*head == NULL) {
*head = node;
} else {
tail -next = node;
}
} else {
free(node);
node = NULL;
}
}
return node;
}

/* END type_1.c */
--
pete
Jul 18 '07 #6

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

Similar topics

1
by: opt_inf_env | last post by:
Hello, I have started to study sessions and as I understood it works in the following way: Let us consider two files first.php and second.php. By clicking on a link in the file first.php user...
7
by: joshsackett | last post by:
All, I am trying to test an attack against a web page. The VBScript runs 2 queries against the database; the first must succeed before the second runs. Here is the code: 1st- select * from...
1
by: Justin | last post by:
We currently have a solution to this coded in VBA in Excel, but the 255 column limitation, general slowness, and instability of Excel are rapidly becoming problems. Access has been suggested as a...
9
by: HK | last post by:
My website emails me when it raises an exception. I'm getting about 10 emails per day that look similar to this, but in each, the IP address and port, and the email-looking stuff, are different. ...
161
by: Dan Lenski | last post by:
Hi all, I'm a recent, belated convert from Perl. I work in a physics lab and have been using Python to automate a lot of measurement equipment lately. It works fabulously for this purpose. ...
0
by: candra | last post by:
Learn What Hackers Know? -General Hacking Information -Password Security -Scanning, Fingerprinting And Similar Techniques -How Hackers Attack Numerous Internet Services -How Hackers Attack Web...
4
by: pangsans | last post by:
hai i have a problem here because of a trojan attack..its called trojan agent horse.ACNS i googled it and could not find any relevant information. this trojan does not let me run anything.for...
2
by: praveenb000 | last post by:
Hi, we are getting reported attack site while browsing these sites http://generalaircon.net/ http://lsinter.com/ Please provide us solution for this problem
15
by: learner247 | last post by:
Hi, I am learning csharp and have a question: My question is about handling a receiving socket. I use the backgroundworker class for multithreading. In the doWork event there is a while...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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...
0
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,...
0
Oralloy
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,...
0
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...
0
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,...

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.