You are viewing our Forum Archives. To view or take place in current topics click here.
[Solved] C scanf troubles
Posted:

[Solved] C scanf troublesPosted:

CriticaI
  • Summer 2018
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,743
Reputation Power: 449
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,743
Reputation Power: 449
I'm taking a class on C, so try not to roast me too much.
Anyways, I have a file i'm supposed to input into my program that looks like this.

bdblack:pwbdblack:328:328:cube 33:/home/bdblack:/bin/bash
bswhite:pwbswhite:329:329:cube 34:/home/bswhite:/bin/bash
sjgreen:pwsjgreen:330:330:cube 35:/home/sjgreen:/bin/bash


and i'm supposed to grab all the data between the colons and spit it back out.
It works on my linux machine, but on mac it doesn't?
I'm pretty sure it's the way it reads the end of the line, but i'm not sure.

Someone please help. Explain like I'm 5.

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

#define SHORT 32
#define LONG 128


int main()
{
    char username[SHORT], password[LONG], gecos[SHORT], home[LONG], shell[LONG], newline[SHORT];
    int uid=0, gid=0;

    while (scanf("%[^:]:%[^:]:%i:%i:%[^:]:%[^:]:%s ",
           username, password, &uid, &gid, gecos, home, shell) != EOF){
        printf("\nusername:\t%s\n", username);
        printf("password:\t%s\n", password);
        printf("uid:\t\t%i\n", uid);
        printf("gid:\t\t%i\n", gid);
        printf("gecos:\t\t%s\n", gecos);
        printf("home:\t\t%s\n", home);
        printf("shell:\t\t%s\n", shell);
    }

    return(0);
}


what it outputs on linux
[ Register or Signin to view external links. ]


Last edited by CriticaI ; edited 1 time in total
#2. Posted:
speed
  • Winter 2020
Status: Offline
Joined: Jun 11, 200914Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Status: Offline
Joined: Jun 11, 200914Year Member
Posts: 9,897
Reputation Power: 3160
Motto: "I'l no I grew up to fast speed I no u will be little famous" - Famous_Energy
Seems to work fine on my MacBook.

[ Register or Signin to view external links. ]

How are you compiling it?
#3. Posted:
tortuga
  • Wizard
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Works fine for me too using gcc v4.8.4.

Are you getting any errors when trying to run it on your Mac, or no?
#4. Posted:
CriticaI
  • Summer 2018
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,743
Reputation Power: 449
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,743
Reputation Power: 449
I'm using clang.
Do different compilers have different output?


Never mind. I'm using Atom to write my code, and Atom adds an extra line to every file for some reason.
that extra line was throwing off my results.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.