By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,872 Members | 2,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,872 IT Pros & Developers. It's quick & easy.

I/O help...I think,,,

P: 3
This part of C always gets me...it shouldn't...but it does, so here I go:
I've looked everywhere but I can't find the answer anwhere

void Arrays (int numArray[], int quantity[])

{
FILE *fp;
fp = fopen("stock.dat", "rb+");
fclose(fp);
}

if I wanted to put the data from the file into the two arrays how would I go about doing this? I can only find the explanations of to read in one..how would I go about reading the record layout in?
Aug 6 '07 #1
Share this Question
Share on Google+
6 Replies


Banfa
Expert Mod 5K+
P: 8,916

weaknessforcats
Expert Mod 5K+
P: 9,197
This code:
void Arrays (int numArray[], int quantity[])

{
FILE *fp;
fp = fopen("stock.dat", "rb+");
fclose(fp);
}
will be hard for you to use. That is, when you pass an array to a function, all that is passed is the address of element 0. So here you have two int pointers for your two arrays but you do not have the number of elements in these arrays.

You need to tell fread how much to read.
Aug 7 '07 #3

P: 3
I think I'm figuring it out...if I wanted to scan the first half of the line, which is set up 348247:title. How would I accomplish this...I've tried this...but it does not seem to be working...(the title which it is comparing to is passed from another fucntion..)

int search(char title[81], int *stocknum[])
{
int j;
char tite[81];
FILE *va;
va = fopen("/Users/cap412/Desktop/videos.dat", "r");
for (j = 0; j < 50; j++){
fscanf(va, "%s", tite[j]);
if(strncmp(tite, title, 81) == 0)
fscanf(va, "%[0-9]", &stocknum[j]);
fclose(va);
}
}
Aug 10 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
The first thing is to stop using fscanf(). This thing skips whitespace.

Use fread() and read in the correct number of bytes or use fgets() which will read in an entire line of text, which you can then parse.
Aug 10 '07 #5

P: 3
The first thing is to stop using fscanf(). This thing skips whitespace.

Use fread() and read in the correct number of bytes or use fgets() which will read in an entire line of text, which you can then parse.
okay, so it can read in, only, the line where the text is located?
Aug 10 '07 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
I assume you know the file format. Yes?

Let's assume the file format is two arrays of 10 ints each.

Expand|Select|Wrap|Line Numbers
  1. int readbuffer[20]
  2.  
  3. fread(readbuffer, 20 * sizeof(int), 1, myfile);
  4.  
Then just distribute the readbuffer array to your two arrays. You know that the first array is readbuffer[0] through readbuffer[9], etc.

I'm just guessing here as I don't know your file format.
Aug 11 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.