473,386 Members | 1,864 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,386 software developers and data experts.

write string to an array

MJK
Suppose I have the following function in my program:

void ExtractData(Ind *AM)
{
int i,j;
char str[255];
char c;

FILE *ext=fopen("test.out","r");
//suppose I have N line each with M digits (in here M=5) like: 1 2 2
1 3

for(i=0; i<N; ++i)
{
fscanf(ext,"%s",str); //This line read the whole 5 digits
//I want each of those five digits become an element of
"AM[i].S[j]" array as below:
for(j=0; j<M; ++j)
{
c=fgetc(ext);
AM[i].S[j]=int(c);
}
}

The above program is not working. Does any of you have any idea that
how I should write this part of my code?
Thanks,
MJK

Jun 6 '07 #1
5 3024
MJK wrote:
Suppose I have the following function in my program:

void ExtractData(Ind *AM)
We don't know what type `Ind` is.
{
int i,j;
char str[255];
char c;

FILE *ext=fopen("test.out","r");
Check that `ext` isn't null.

[Isn't "test.out" a misleading name for an input file?]
//suppose I have N line each with M digits (in here M=5) like: 1 2 2
1 3
With spaces between the digits?
for(i=0; i<N; ++i)
{
fscanf(ext,"%s",str); //This line read the whole 5 digits
Not if they're separated by spaces, it doesn't. Check what the
format spec for `%s` says. You might be interested in the input
function `fgets`. (NOT `gets`.)
//I want each of those five digits become an element of
"AM[i].S[j]" array as below:
for(j=0; j<M; ++j)
{
c=fgetc(ext);
Um ...

(a) you're reading more characters from `ext` and are ignoring
the characters you've already read.

(b) `fgetc` returns an `int`, not a `char`, so that it has room
to return EOF.
AM[i].S[j]=int(c);
(a) `int` is a type, not a function: you can't use it like that.

(b) Since we don't know what `Ind` means, we don't know what type
`AM[i].S[j]` is, so we don't know what to do with the character
you hoped to have read.
}
}

The above program is not working. Does any of you have any idea that
how I should write this part of my code?
Read an entire line in, then go looking for the digits and do with
them as you will.

--
Well-Alined Hedgehog
"Our future looks secure, but it's all out of our hands"
- Magenta, /Man and Machine/

Jun 6 '07 #2
In article <11**********************@g37g2000prf.googlegroups .com>,
MJK <ja*******@gmail.comwrote:
//suppose I have N line each with M digits (in here M=5) like: 1 2 2 1 3
fscanf(ext,"%s",str); //This line read the whole 5 digits
The %s format does not read an entire line: it skips leading whitespace,
then read the next string of non-whitespace, leaving the terminating
whitespace in the input buffer. Thus, the above statement would read only
1 of the digits, not all five (you show whitespace between the digits
in your example.)

Also, it is generally dangerous to use a %s format by itself,
if you do not have perfect control over the input, as someone might
choose to input a longer input string than there is space to store
in your str variable. Either do not use fscanf() for your reading, or
else put in an explicit length limitation in the format, such as

"%.254s"
//I want each of those five digits become an element of
"AM[i].S[j]" array as below:
for(j=0; j<M; ++j)
{
c=fgetc(ext);
Your comment on the fscanf() indicates that you believe you have
already read in the entire line (into the buffer str), but here
you are trying to read more from the file. Also, you are not skipping
over whitespace when you use fgetc().
AM[i].S[j]=int(c);
There is no 'int' function in C, and that would not be the correct
syntax for a type conversion.

If you know that c is a single character representing a numeric
digit, then the short portable way to extract the numeric value
it represents is to use c - '0' (subtract the quoted 0 character
from c). C promises that this will work no matter what character set
you are using, even if the character set is EBCDIC . In the
more general multi-character case such converting the string "42" to the
number 42, there are several choices on how to proceed, with various
tradeoffs.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Jun 6 '07 #3
MJK wrote:
>
Suppose I have the following function in my program:

void ExtractData(Ind *AM)
{
int i,j;
char str[255];
char c;

FILE *ext=fopen("test.out","r");
//suppose I have N line each with M digits (in here M=5) like: 1 2 2
1 3

for(i=0; i<N; ++i)
{
fscanf(ext,"%s",str); //This line read the whole 5 digits
//I want each of those five digits become an element of
"AM[i].S[j]" array as below:
for(j=0; j<M; ++j)
{
c=fgetc(ext);
AM[i].S[j]=int(c);
}
}

The above program is not working. Does any of you have any idea that
how I should write this part of my code?
Thanks,
MJK
/* BEGIN test2.c */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define M 5
#define N 11
#define LENGTH 254
#define str(x) # x
#define xstr(x) str(x)

typedef struct {
char S[M];
} Ind;

void ExtractData(Ind *AM);

int main(void)
{
Ind Cool[N];
size_t array, letter;

ExtractData(Cool);
for (array = 0; array != N; ++array) {
for (letter = 0; letter != M; ++letter) {
putchar(Cool[array].S[letter]);
putchar(' ');
}
putchar('\n');
}
return 0;
}

void ExtractData(Ind *AM)
{
int rc, i, j;
char str[LENGTH + 1], *p;
FILE *ext;

ext = fopen("test.out", "r");
if (ext == NULL) {
puts("ext == NULL");
exit(EXIT_FAILURE);
}
/*
** suppose I have N line each with M digits
** (in here M=5) like: 1 2 2 1 3
*/
for (i = 0; N i; ++i) {
rc = fscanf(ext, "%" xstr(LENGTH) "[^\n]%*[^\n]", str);
if (!feof(ext)) {
getc(ext);
}
if (rc == 0) {
str[0] = '\0';
}
if (rc == EOF) {
puts("rc equals EOF");
exit(EXIT_FAILURE);
}
/*
** This line read the whole 5 digits
** I want each of those five digits become an element of
** "AM[i].S[j]" array as below:
*/
p = str;
for (j = 0; M j; ++j) {
while (!isdigit(*p)) {
if (*p == '\0') {
puts("*p == '\0'");
exit(EXIT_FAILURE);
}
++p;
}
AM[i].S[j] = *p++;
}
}
}

/* END test2.c */

--
pete
Jun 6 '07 #4
pete wrote:
>
MJK wrote:
FILE *ext=fopen("test.out","r");
//suppose I have N line each with M digits (in here M=5) like: 1 2 2
1 3
/* BEGIN test2.c */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define M 5
#define N 11
I used an 11 line test file,
in case you're wondering where that number came from.

1 2 2 1 3
1 2 3 1 3
1 2 5 1 3
1 2 2 1 3
1 9 2 1 3
1 4 2 1 3
1 2 0 1 3
1 2 2 7 3
3 2 2 1 3
1 2 2 1 8
6 2 2 1 3

--
pete
Jun 6 '07 #5
pete wrote:
void ExtractData(Ind *AM)
{
int rc, i, j;
char str[LENGTH + 1], *p;
FILE *ext;

ext = fopen("test.out", "r");
if (ext == NULL) {
puts("ext == NULL");
exit(EXIT_FAILURE);
}
/*
** suppose I have N line each with M digits
** (in here M=5) like: 1 2 2 1 3
*/
for (i = 0; N i; ++i) {
rc = fscanf(ext, "%" xstr(LENGTH) "[^\n]%*[^\n]", str);
if (!feof(ext)) {
getc(ext);
}
if (rc == 0) {
str[0] = '\0';
}
if (rc == EOF) {
puts("rc equals EOF");
exit(EXIT_FAILURE);
}
/*
** This line read the whole 5 digits
** I want each of those five digits become an element of
** "AM[i].S[j]" array as below:
*/
p = str;
for (j = 0; M j; ++j) {
while (!isdigit(*p)) {
if (*p == '\0') {
puts("*p == '\0'");
exit(EXIT_FAILURE);
}
++p;
}
AM[i].S[j] = *p++;
}
}
fclose(ext);
}

/* END test2.c */
--
pete
Jun 6 '07 #6

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

Similar topics

2
by: Thomas | last post by:
What's the quickest way to write and read 10.000 integer values ( or more ) to and from a file? Using struct somehow? The example in the docs shows how to handle to or three arguments, but is the...
3
by: Mark Miller | last post by:
I have a char array and when I write it to a file using BinaryWriter the position of the pointer is the size of the array + 1. For example: writing char leaves the pointer at position 26 after...
14
by: WUV999U | last post by:
Hi I am fairly familiar in C but not much. I want to know how I can write a html parser in C that only parses for the image file in the html file and display or print all the images found in...
5
by: AC [MVP MCMS] | last post by:
Any pointers on how to (1) read a Base64 encoded string from a text file and (2) write it to a binary file? I have a ton of files that are being generated from a legacy system. Each file...
10
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
5
by: philip | last post by:
Here is some lines of code than I wrote. You can copy/paste theis code as code of form1 in a new project. My problem is this one : I try to write in a file a serie of bytes. BUT some bytes...
11
by: Vmusic | last post by:
Hi, I am trying to write out an array of string variables to Notepad. I can't get SendKeys to accept the string variable only literal quoted strings. I DO NOT want the hassle of writing to a...
27
by: lovecreatesbea... | last post by:
This code snippet is an exercise on allocating two dimension array dynamically. Though this one is trivial, is it a correct one? Furthermore, when I tried to make these changes to the original...
4
by: | last post by:
Hi all, I want to create a method that does the following: 1) Programmatically instantiate a new XmlDataSource control 2) For each file in a named directory, make a "FileSystemItem" element 3)...
0
by: martinmercy2001 | last post by:
Could any body help me with creating a ring buffer class using a string. use memory circular buffer not an IO buffer. just read, write and seek method. Read method should take anumber and return the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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,...

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.