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

Pages: 1-4041-

[beginner] C reading lines, and numbers

Name: Anonymous 2009-08-29 8:56

I have a file in which ever line of text is terminated with a number. I'd like to extract the text and the number to variables, how do I do that?

Name: Anonymous 2009-08-29 8:58

Go to /pr/. Ask there.

Name: Anonymous 2009-08-29 9:00

You should write a program to do it.

Name: Anonymous 2009-08-29 9:04

>>3
you know, I'd have never thought of that.

Name: Anonymous 2009-08-29 9:05

if(c <= '9' && c >= '0')
that's all i'm saying.
go to /pr/

Name: Anonymous 2009-08-29 9:09

awk(1)

Name: Anonymous 2009-08-29 9:10

use regexen

Name: Anonymous 2009-08-29 9:19

Two lines in Python.

Name: Anonymous 2009-08-29 9:23

>>8
One line in Bash.

Name: Anonymous 2009-08-29 9:26

>>9
Half a line in Perl.

Name: Anonymous 2009-08-29 9:29

No lines in HASKAL.

Name: Anonymous 2009-08-29 9:29

Five screenfuls in Java.

Name: Anonymous 2009-08-29 9:31

>>11
Actually you could do it in a single one but it would be imaginary.

Name: Anonymous 2009-08-29 9:38

>>13
An imaginary line in a fictional language? Who'da thunk it?

Name: Anonymous 2009-08-29 9:55

>>14
There are no fictional languages.

Name: Anonymous 2009-08-29 10:13

>>15

ada started from a fiction

Name: Anonymous 2009-08-29 10:23

>>15
Haskell is the world's leading fictional programming language, everyone know's that

Name: Anonymous 2009-08-29 10:23

>>16
The fiction of a woman who knew math?

Name: Anonymous 2009-08-29 13:29


int doit() {
  char buf[64];
  int n = 0;
  size_t i, x = 1;

  if(fgets(buf, 64, stdin)) {
    i = strlen(buf);
    while(i && isdigit((unsigned char)buf[--i]))
      n += (buf[i] - '0') * x, x *= 10;
    if(buf[i] == '-') return -n;
    return n;
  }
}

Name: Anonymous 2009-08-29 13:31

>>19
notice the newline that usually will be there with fgets (unless the user types something like foo bar-123^D), is not trimmed, on purpose. If you desire such functionality implement it yourself.

Name: TimeTravelingAdaLovelace 2009-08-29 13:32

>>18
Nice try.

Name: Anonymous 2009-08-29 13:40

>>20
notice the enlightment that usually will be there with SICP (unless the wizard cast something like (macrolet ((spell () `(,@(loop (princ '|SUSSMAN |)))) (spell))), is not permanent, on purpose. If you desire such functionality implement it yourself.

Name: Anonymous 2009-08-29 13:41

>>19
You know, variable names can be longer than three characters, even in C.

Name: Anonymous 2009-08-29 13:48

>>23
Yes I'm aware. In fact, I even took the privilege to name a four character function in that program, `doit', admittedly elusive to your punny excuse-for-a-brain.

Name: Anonymous 2009-08-29 13:50


int num = 0;
char c;
while((c = getchar()) >= '0' && c <= '9'){
   num += (c - '0') * 10;
}

dus dis wurk?
to sav cher raray is easy. u dun ned me to do dat fur u

Name: Anonymous 2009-08-29 14:01

>>25
Well if 123 means 321, sure, it dus wurk.

Name: Anonymous 2009-08-29 14:27

>>23
char BUFFA[64];

Name: Anonymous 2009-08-29 15:07

Sometimes I wonder what happens when a haskellette needs more than 26 variables. Does she use UTF8 or something?

Name: Anonymous 2009-08-29 15:08

>>24
But that's a function name, not a variable name, admittedly elusive to your punny excuse-for-a-brain. That's a pretty good insult, by the way, so right back at ya, bro.

Name: Anonymous 2009-08-29 15:09

>>28
she uses a', a'', etc. desu yo~~

Name: Anonymous 2009-08-29 15:09

>>29
I don't know about C, but functions and variables are the same where I live.

Name: Anonymous 2009-08-29 15:11

I don't think this is punny at all.

Name: Anonymous 2009-08-29 15:11

>>30
Nice try, but primes are reserved for names of the iterative loop functions that take additional fixed starting arguments.

Name: Anonymous 2009-08-29 15:12

>>33
not necessarily desu yo~~

Name: Anonymous 2009-08-29 16:06

she
:D

Name: Anonymous 2009-08-29 21:43

>>26
don't be daft, you're supposed to read the line of text backwards

Name: Anonymous 2009-08-30 1:19

>>33
In exposed function names, it generally indicates strictness (e.g. foldl'). In a where clause, it generally indicates what >>33-san said.

In variable names, it usually means the updated value of an existing variable, or a value corresponding to it in some otherway.

let a' = f a
          a'' = g a'
      in h a a' a''


(x,y) + (x',y') = (x + x', y + y')

As can be see at http://hackage.haskell.org/packages/archive/ghc-prim/0.1.0.0/doc/html/src/GHC-Tuple.html#%28%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%29, clearly _ is also an option.

Name: Anonymous 2009-08-30 9:34

>>37
you should be using pointfree style for those desu yo~~

Name: Anonymous 2009-08-30 19:02

>>37
DELETE * FROM front_page WHERE posts ARE TOO LONG;

Name: Anonymous 2009-08-30 19:03

>>37
Thread.Sage ();

Name: Anonymous 2009-08-30 19:07

>>37
from /prog/ import *

shitty_posts = [x for x in [y for y in threads if y.on_front_page()] if x.shitty()]
for p in shitty_posts:
    p.sage()

Name: Anonymous 2009-08-30 19:08

>>37
(mapcar #'sage *shitty-posts*)

Name: Anonymous 2009-08-30 23:43

>>39-42
You must be using some strange definition of ``too long'' that was hitherto unaware of.

Name: Anonymous 2010-10-26 22:37

Name: Anonymous 2011-02-02 23:10

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