open file and add node to linked list

This is a discussion on open file and add node to linked list within the C Programming forums, part of the General Programming Boards category; I wrote some function to read input.txt and add the elements in this file to node, but it doesn't work ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    5

    Question open file and add node to linked list

    I wrote some function to read input.txt and add the elements in this file to node, but it doesn't work anymore
    would you mind check and figure out my mistake, thx alot

    Code:
     In function `input': 
    71 [Warning] assignment makes integer from pointer without a cast 
    75  subscripted value is neither array nor pointer 
    
     In function `main': 
    101  too few arguments to function `input'
    Code:
    Input.txt 
    6 2 4 3 
    -2 4 1 -3 7 6
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <conio.h>
    
        int rows, cols,row, col, k, i, element[10], p[10],array2; 
        int n,a,g,r;    
        int **rp, *ap; 
        
    typedef struct node *NodePtr;
    struct node 
        {
        int info;
        NodePtr link;
        };
    void PrintContent(NodePtr pList)
        {
        NodePtr p=pList;
        
        while (p!=NULL)
        { printf("%d ",p->info);
        p=p->link;
        }
        printf("\n\n");
        }
            
    NodePtr CreateNode(int data)
        {
        NodePtr pA = (NodePtr) malloc(sizeof(struct node));
        pA->info=data;
        pA->link=NULL;
        return pA;
        }
     
    void InsertNode(NodePtr *ppList, NodePtr newnode)// insert the newnode into the list pointed by *ppList. 
        {  if (*ppList==NULL) 
          // the list is empty 
          {   
            *ppList = newnode; 
          } 
          else if (newnode->info < (*ppList)->info) 
          // insert at the beginning of the list  
          {  newnode->link = *ppList; 
            *ppList = newnode; 
          } 
          else  
          {  NodePtr p=*ppList; 
            while (p->link!=NULL && p->link->info < newnode->info) 
              p = p->link; 
            // p will stop at the last node, or at the node  
            //    after which we should insert the new node. 
            newnode->link = p->link; 
            p->link = newnode; 
          }   
        } 
            
    void input(FILE *file, char nameInput[]) //Function to read data from file
    {
         int j, i;
         file = fopen(nameInput, "r");
         if(file == NULL){
                 printf("Error:Can't open the file");
                 getch();
         }
         else{
              printf("File opened successfully!\n");
              }
         fscanf (file,"%d",&n);
         fscanf (file,"%d",&a);
         fscanf (file,"%d",&g);
         fscanf (file,"%d",&r);           
         array2 = malloc(n *sizeof(int));
         for (j = 0; j < n; j++){
             if(!feof(file))
             {
                       fscanf(file,"%d",&array2[j]);
             }
         }
         fclose(file);
         }
    
    void output(void)
        {
        int a[100];
        FILE *fpoutput;
        fpoutput=fopen("Output.txt","w");
        
        for (i=0;i<3;i++)
            fprintf(fpoutput,"%d ",a[i]);
            
        fclose(fpoutput);
        }
     
    
        
    
    
    
        
    int main(void)
        {
        input();
        output();
        getch();
        }
    Last edited by blackant; 03-24-2009 at 02:15 AM. Reason: fixx

  2. #2
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Helps if to quickly debug if you specify what type of error you're getting.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    I added the error when I complied it in DEV C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 06:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21