-3

Hello I'm writing a program right now that read in values from files.

It reads in the values as strings by fgets and then I wanted to change one of the strings into integers:

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#define FILSIZE 1024


typedef struct _node
{
unsigned int uid;
char *uname;
struct _node *next;
} *node;



int main(int argc, char* argv[])
{
FILE *pFile; // pointer file
char currentLine[FILSIZE];
bool isListEmpty = true;
node firstNode = NULL;

printf( "\n\nargc: %d\nargv[0]: %s\nargv[1]: %s\n\n", argc, argv[0], argv[1] );


if(argc == 1)
    {
    pFile = stdin;
    }


else if (argc == 2)
    {

    char buffer [50];       
    sprintf(buffer, "%s.txt" ,argv[1]);
    printf("%s", buffer);
    pFile = fopen(buffer, "r");

    }

    if(pFile == NULL )
    {
       printf("Not working");


  exit(0);
    }

int i=1;
int nameCounter = 1;
int colonCounter=0;


 while(!feof( pFile ) && fgets(currentLine, sizeof(currentLine), pFile))
    {



unsigned int nameLength;
unsigned int tempuid;   
unsigned int idstorlek;
node firstNode = malloc(sizeof(struct _node));

char *temporaryString;
    temporaryString = strtok(currentLine, ":");
colonCounter=colonCounter+1;

//printf(" fungera pls");
printf(" %d Namnordning %s \n", nameCounter, temporaryString);

nameLength = strlen(temporaryString);
firstNode->uname = malloc((nameLength+1) * sizeof(char));   
strcpy(firstNode->uname, temporaryString);




printf("NAMNET: %s \n", temporaryString);


while(temporaryString != NULL)
    {
    printf(" %s \n", temporaryString);

    if(colonCounter == 3)
        {

        int tempuid=atoi(temporaryString);
        // idstorlek = sizeof(tempuid);
         // firstNode->uid = malloc(4);
        printf(" IDN: %d \n", tempuid);             
        firstNode->uid = tempuid;
        printf("firstNodeid %d", firstNode->uid);
        }

    temporaryString = strtok(NULL, ":");        
    colonCounter=colonCounter+1;
    }

    if(colonCounter == 6)
    {

    // printf("FUNGERAR ID: %d NAMN %s \n", tempuid, firstNode->uname);

    }
    printf("%d Row is done \n", i);
    i=i+1;
    nameCounter = nameCounter+1;
    colonCounter = 0;



}



}           

But when I write it out I get:

1 Namnordning mr 
NAMNET: mr 
 mr 
 x 
 1171 
 IDN: 1171 
firstNodeid 1171 1101 
 Mikael R�nnar 
 /Home/staff/mr 
 /usr/local/bin/tcsh

1 Row is done 
 2 Namnordning axelsson 
NAMNET: axelsson 
 axelsson 
 x 
 12856 
 IDN: 12856 
firstNodeid 12856 1101 
 Bj�rn Axelsson 
 /Home/staff/axelsson 
 /usr/local/bin/tcsh

2 Row is done 
 3 Namnordning gabriel 
NAMNET: gabriel 
 gabriel 
 x 
 16928 
 IDN: 16928 
firstNodeid 16928 1101 
 Gabriel Jonsson 
 /Home/staff/gabriel 
 /usr/local/bin/tcsh

3 Row is done  
 4 Namnordning set 
NAMNET: set 
 set 
 x 
 12037 
 IDN: 12037 
firstNodeid 12037 1101 
 Set Norman 
 /Home/staff/set 
 /usr/local/bin/tcsh

4 Row is done 
 5 Namnordning dahlin 
NAMNET: dahlin 
 dahlin 
 x 
 12928 
 IDN: 12928 
firstNodeid 12928 1101 
 Fredrik Dahlin 
 /Home/staff/dahlin 
 /usr/local/bin/tcsh

5 Row is done 
 6 Namnordning fahlgren 
NAMNET: fahlgren 
 fahlgren 
 x 
 17847 
 IDN: 17847 
firstNodeid 17847 1101 
 Daniel Fahlgren 
 /Home/staff/fahlgren 
 /usr/local/bin/tcsh 
6 Row is done  

Why do I get 1101 too there? The other tempuid part only gives me the correct id. Am I not locating the memory right? (I been trying to and it only gives me weird errors, with the idsize part).

Joe
  • 11
  • 4

2 Answers2

0

Normally you should post a small, complete example demonstrating the behavior (you'll often find the bug yourself when doing this).

But it turns out that you have posted enough information to give a pretty clear indicator: your code is not printing out an extra number. Instead, it's merely failing to print a newline; the next piece of code that does output will start writing on the same line.

  • I'll put up the whole code I guess. But no this error happens for every "node". Here I'll put up the code and what I get in the terminal – Joe Oct 16 '15 at 15:36
0

It's because you're not flushing the print buffer after you call

printf("firstNodeid %d", firstNode->uid);

printf stores a buffer of characters rather than writing a string to stdout as soon as it gets them to avoid the overhead of calling the lower-level write function too often.

Instead calling:

printf("firstNodeid %d\n", firstNode->uid);

Should fix your problem because "\n" will add a newline to the string, and flush the output.

Edit: You should also be careful with types. You're defining tempuid as an int:

int tempuid=atoi(temporaryString);
// idstorlek = sizeof(tempuid);
// firstNode->uid = malloc(4);
printf(" IDN: %d \n", tempuid);             
firstNode->uid = tempuid;
printf("firstNodeid %d", firstNode->uid);

But defining it as an unsigned int in your node struct.

Byte Lab
  • 1,576
  • 2
  • 17
  • 42
  • Flushing is irrelevant; if he called `fflush(stdout);` he'd get the same results because of the failure to print a newline. –  Oct 16 '15 at 15:37
  • @Hurkyl I completely disagree. OP was confused because he thought that extra information was getting printed. In reality, it's just that printf wasn't writing his second ID to stdout until the buffer was flushed later. Simply saying "add a newline" doesn't give him/her any intuition for the buffering taking place. Sure, at a certain point he/she will see the same output, but the true reason for the confusion is a lack of understanding of the buffer in printf. – Byte Lab Oct 16 '15 at 15:41
  • All \n does is making the 1101 end up on a new line. I posted the whole code but I am still very confused about why it's adding 1101. – Joe Oct 16 '15 at 15:50
  • @Joe That's because you're printing a 1101 at some point after you call `printf("firstNodeid %d\n", firstNode->uid);`. – Byte Lab Oct 16 '15 at 15:53
  • I'm looking through the code over and over. I don't see it anywhere. Also it prints everything else correct. If you can find wherever I am doing that I'd really appreciate it. But I don't see it anywhere – Joe Oct 16 '15 at 15:58
  • @Joe take a look at the type comment I made above. See if that fixes it. – Byte Lab Oct 16 '15 at 16:00
  • 2
    Wow. Yeah you're right. The file I'm reading in from actually has 1101 in each row at exactly the same place. I tried make it not print out the firstNode part and it still had 1101. I then ended up checking the file and it turns out it is supposed to be that way. Thank you for the help :D – Joe Oct 16 '15 at 16:05
  • @Joe I'm very glad to have helped you. Welcome to SO, and keep coming back! When you're a jedi C developer remember to help other people here who are just starting out like you :-) – Byte Lab Oct 16 '15 at 16:08
  • Will do. I also changed it so unsigned int. – Joe Oct 16 '15 at 16:10
  • @Joe Yes, I believe so. In general it's best to be consistent and explicit. C has obtuse rules about implicit casting when you do assignments between various types and it's easy to shoot yourself in the foot. I would read chapter 2 of K & R to get a better sense of how it works. Again though in general the best rule of thumb is to be consistent between assignments, and explicitly cast if you definitely want to change a type. – Byte Lab Oct 16 '15 at 16:10