d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Iso C Programming Help
Add Reply New Topic New Poll
Member
Posts: 2,930
Joined: May 4 2007
Gold: 1,028.00
Feb 6 2017 06:42pm
Need to read a file into an array.

Looks like:
1 _ 2 3
2 1 _ 4
3 2 1 _
4 3 _ 1

Spaces between each character, numbers, underlines.
I can figure the rest of it out, I'm just having trouble with the file IO part. I can manipulate the data from there.
I've looked at MAN pages for fprintf, getline, and strtok and none of them SEEM to do what I need them to do, unless I'm misunderstanding something.
I need to read that board into a 4x4 array. I can take over from there.

Any advice?
Member
Posts: 3,324
Joined: Mar 1 2008
Gold: 0.69
Feb 6 2017 06:52pm
You can sort it on input, or parse each line into strings then sort into the array, i havn't done coding in a while, there should be similar questions on stackoverflow
Member
Posts: 2,930
Joined: May 4 2007
Gold: 1,028.00
Feb 6 2017 06:56pm
I've spent the past 2 hours on stackoverflow. Everthing having to do with C on stackoverflow is WAAAAYYYYYY above and beyond what I've learned so far.
I don't even know how to parse a string into my program from the file. Like... I can't even make my program reproduce ONE line from the file directly into a printf statement, I don't even know how to do that.

I'm used to C++ and Java, this is my first real stab at C.
Member
Posts: 3,324
Joined: Mar 1 2008
Gold: 0.69
Feb 6 2017 06:58pm
yeah i could tell you hot to do this on c++ and java, i havnt done any c sorry
Member
Posts: 207
Joined: Sep 14 2010
Gold: 2,022.00
Feb 6 2017 06:59pm
The problem of parsing the input can be broken into two different problems:

Identifying consecutive series of numerical characters [0-9] or _, and identifying what those numbers are.

Code
#define LINE_SIZE <larger than your largest expected line>

char line[LINE_SIZE];

// Assuming your reading from stdin
while (fgets(line, LINE_SIZE, stdin)) {
int i;
int last_digit = 0;

// Identify digits and process
for (i = 0; i < LINE_SIZE && line[i] != '\0'; i++) {

// Check for digit [0-9]
if (isdigit(line[i])) {
// Add line[i] to the list of digits I've found
} else if (line[i] == '_') {
// Handle that I have an _
} else {
// Handle that I've reached the end of a number or _ here
}
}
}


Once you have sequences of digits there are many functions that will parse them:
sscanf("%d");
atoi(string);

Does this help clarify the solution?

This post was edited by ddevec on Feb 6 2017 07:08pm
Member
Posts: 17,833
Joined: Sep 1 2016
Gold: 10,665.00
Feb 6 2017 07:08pm
The biggest question everyone should have asked you is... in the file array, are these numbers restricted to a single character or can you have 10 1 5 _? This will change how you code this quite a bit.
Member
Posts: 2,930
Joined: May 4 2007
Gold: 1,028.00
Feb 6 2017 07:55pm
Quote (GatheringHero @ Feb 6 2017 07:08pm)
The biggest question everyone should have asked you is... in the file array, are these numbers restricted to a single character or can you have 10 1 5 _? This will change how you code this quite a bit.


it can only be digits 1-4 or an underscore. Its a 4x4 sudoku board.
Go Back To Homework Help Topic List
Add Reply New Topic New Poll