Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

read numbers from file and assign it to corresponding variables in c

Posted on 2007-03-19
7
189 Views
Last Modified: 2010-04-15
I have a txt file with numbers such as...

4
10
3 23 1 39 48
9 3 92 9 12
etc

the first row represents something, the second number in row two represent something else.
the third row and corresponding number each have it's own functionality

here is what i need to do but can't figure out how in C
int a = (the number in first row, which is 4 in example);
int b = (the number in second row, 10 in example);
int[number of numbers in row 3] c = {in row three - first num, second num, third num...etc} = {3,23,1,39,48};
int[number of numbers in row 4] d = {......}

How can I do this?
0
Comment
Question by:shpark82
  • 3
  • 2
7 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18753403
Take a look at fscanf() :

http://www.cplusplus.com/reference/clibrary/cstdio/fscanf.html

Let us know if there is something that is not clear on that page. Don't forget to take a look at the example at the bottom of the page ...
0
 
LVL 7

Accepted Solution

by:
nixfreak earned 500 total points
ID: 18754257
I hope I haven't misunderstood your question. The below program will read the files contents and put values an array. Instead of the arrays a, b, c, d  you have entry[0], entry[1], entry[2], entry[3] respectively. For example:
c[3] = 39
entry[2][3]=39

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

#define MAXCOL  4096

int
main(int argc, char *argv[])
{
        char    *line;
        int     *entry[MAXCOL];
        char    *token, *ret;
        FILE    *file;

        file = fopen("argv[1]", "r");

        for ( i = 0; ; i++ ) {
                entry[i] = calloc(MAXCOL, sizeof(int));
                if ( fgets(line, MAXCOL, file) == NULL)
                        break;
                token = strtok(line, " ");
                entry[i][0] = atoi(token);
                for ( j = 1; (token = strtok(NULL, " ")) != NULL; j++ )
                        entry[i][j] = atoi(token);
        }

        fclose(file);

        return 0;
}

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18755120
nixfreak, please don't post complete code solutions for questions that are very likely homework assignments.

The point of a homework assignment is that you learn something by solving it yourself. If you give the solution, you've robbed shpark82 of that opportunity.

Instead, you are allowed to give hints and pointers whenever and wherever the asker has problems with the assignment.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18755123
@shpark82 : I would suggest to try to solve this yourself without looking at nixfreak's code. You'll learn a lot more, and gain a lot more satisfaction.
0
 
LVL 7

Expert Comment

by:nixfreak
ID: 18913637
Paul,

While I agree with infinity08's advice, I think I did give a correct solution.
0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Microsoft vcsetup.exe and vcssetup.exe 8 435
Where is my core dump file in Ubuntu? 12 454
How to access ANSI/IEEE Std 754 or equivalent information ? 3 73
outlook office 365 8 144
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question