Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7166795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:28:15+00:00 2026-05-28T14:28:15+00:00

I read from file store the data into a list; I want to add

  • 0

I read from file store the data into a list;

I want to add to the front of the list as I read in.

The insertion seems to work, but my print function causes a -1 return.
This is my code:

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

This is my list

typedef struct node{
int x,y,v;
struct node* next;
}node;

This is my insert:

node* insert(node* L, int x, int y, int v){
node* new= (node*)malloc(sizeof(node*));
new->x = x;
new->y = y;
new->v = v;
new->next=NULL;
if(L==NULL){
    L=new;
}
else{
new->next=L;
L=new;
}   
return L;
}

The problem seems to be here:

void printList(node* L){
node* c=NULL;
c=L;
while(c != NULL){
printf("x=%d, y=%d, v=%d\n", c->x, c->y, c->v);
c=c->next;
}
}

The main:

int main(int argc, char* argv[]){
FILE* in;
int h, w;
int x, y, v;
in = fopen(argv[1], "r");
if(in == NULL )
     {
           puts ( "cannot open file" ) ;
           exit(0) ;
     }
fscanf (in, "%d,%d\n", &h, &w);
printf("%d,%d\n", h, w);
node* L=NULL;
while( !feof (in) ){
    fscanf (in, "%d,%d,%d\n", &x, &y, &v);
    L=insert(L, x, y, v);
    //printf("x=%d, y=%d, v=%d\n", L->x, L->y, L->v);
    //printf("%d,%d,%d\n", x, y, v);
}

printList(L);
return 0;
}

What’s wrong, please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:28:16+00:00Added an answer on May 28, 2026 at 2:28 pm
    node* new= (node*)malloc(sizeof(node*));
    

    You’re allocating size for a pointer to node (4 bytes on a 32bit machine), but what you want is a node (16 bytes, or sizeof(node)).

    Also, I’d say that even if C doesn’t care, I’d avoid new as a variable name. It is a keyword in many languages,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read each line from a text file and store them in
I need to store data into files inside .jar file and read it again.
Hi there I have to read data from text file into the array in
I'm trying to read data in from a binary file and then store in
How do I read data from a file if my file is like this
How do I put the records from the random access file DATA.data into the
I have some C#/Linq code used to merge data from excel file into db,
I have to return data fetched from MySQL table into a php file as
I am trying to read from file: The file is multiline and basically i
The string in question (read from a file): if (true) then { _this =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.