Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Some little help with string formatting in C

Name: Anonymous 2008-03-17 10:41

Hai guise, as mentioned above, I need some little help. Here's the thing:
I've been given a school project and I need to simulate an exam-date-organizer. OOP is prohibited, and I've gotta use C to code the program. Anyways, I've gotta read some text from a file and that's where I've got problem with.
Some parts of the text file has multiple data types in a single line. As in this format: "string,integer,string,integer,string\n". The problem is, the first and last string may (or WILL) contain blank spaces. fscanf function ignores character such as these, so I can't read the values correctly. Putting all string on separate lines is a solution, but I don't wanna put EVERYTHING on single lines, as it would take make it harder to read (for the user). Using a length-based reading function is another solution, but the string lengths may (or WILL) vary.

I made a quick search and found out that I can tell the application NOT to ignore blanks, etc, but it seems I can't use it well enough, as the function goes too far in the file, and skip some values. Here's an example:
The code block I use to read is:
fscanf(f,"\n%[A-z,^ ],%d,%s,%d,%s",lname,&cyear,lcode,&etime,name);
Data types for these variables are:
lname:char*, cyear:int, lcode:char*, etime:int, name:char*
What's written in the file is (on a single line):
Calculus II,0,MAT102,80,Some Guy
(lesson name,class year,lesson code,exam time in minutes,some guy's name)
The values I get for these variables get are:
lname: Calculus II, (including the comma)
cyear: 1
lcode: (nothing)
name: (nothing)
etime: 7

So, for short, I need to read comma-separated values from a single line, including the blanks, and skip to the next line with "\n". How do I do that?

Name: Anonymous 2008-03-17 10:45

split /\s*,\s*/,<FILE>

Name: Anonymous 2008-03-17 11:07

scanf("%[^,],%d,%[^,],%d,%[^,]\n",lname,&cyear,lcode,&etime,name)

Name: Anonymous 2008-03-17 11:16

>>3
The \n in your format string will skip all whitespace at that point, including spaces at the beginning of the next file. Use a fgets + sscanf combo to avoid this problem.

Name: Anonymous 2008-03-17 12:50

Use a state machine.

while((c=getchar())!=EOF) {
 switch(c) {
  case ' ':
  case '\t':
   continue;
  case 'A': case 'B': ...
   /* collect bytes into an array here, checking for overflow as necessary */
   break;
  case ',':
   goto done_first_field;
 }
}
done_first_field:


You don't need to use a state machine completely, some things (like parsing the integers) could be handled with atoi(). But I'd write the main code as a form of state machine.

OOP is prohibited
Good. If you even consider using OOP for something as trivial as this, you should never program again.

Name: Anonymous 2008-03-17 12:54

>>5
Use Haskell.

>>=

Name: Anonymous 2008-03-17 13:11

>>1

Q: The book I've been using, C Programing for the Compleat Idiot, always uses void main().

A: Perhaps its author counts himself among the target audience. Many books unaccountably use void main() in examples, and assert that it's correct. They're wrong, or they're assuming that everyone writes code for systems where it happens to work.

Name: Anonymous 2008-03-17 17:05

>>7h
He probably saw

main() {
 ...
}


and took the absence of return value to mean void (it defaults to int you idiot, read the fuckin standard)

Name: Anonymous 2008-03-17 18:05

>>8
From the point of view of machine code, the return value is whatever happens to be in the accumulator register at the time of return

Name: Anonymous 2008-03-17 18:18

GOTO CONSIDERED HARMFUL

Name: Anonymous 2008-03-17 18:20

>>9
From the point of view of machine code, the return value is whatever happens to be the total state of the system at the time of return.

Name: Anonymous 2008-03-17 19:36

>>11
From the point of view of the OS, the return value is whatever happens to be the length of the admin's neckbeard in inches.

Name: Anonymous 2008-03-17 20:18

>>8
[b][i]EXPERT
HEXADECIMAL
QUOTING
[/i]
[/b]

Name: Anonymous 2008-03-17 23:46

>>12
So is that a float or what?

Name: Anonymous 2008-03-18 0:03

>>14
integer; the value is truncated

Name: Anonymous 2008-03-18 5:42

>>12
From the point of view of me, the return value is emitted by whatever happens to be lodged in my asshole at the time.

Don't change these.
Name: Email:
Entire Thread Thread List