Question about saving results to a txt file from a routine

This is a discussion on Question about saving results to a txt file from a routine within the C Programming forums, part of the General Programming Boards category; Hi guys, I am writing a code to construct the shortest path between 2 points using the floyd warshall algorithm. ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    21

    Unhappy Question about saving results to a txt file from a routine

    Hi guys,
    I am writing a code to construct the shortest path between 2 points using the floyd warshall algorithm. However I am running into trouble while trying to print the results of my getpath routine. Basically what I do is I call the routine from main() using this code:


    Code:
    FILE *ofp=fopen("path.txt","w");
    fprintf(ofp,"    ");
    for (i = 0; i < n; ++i) 
      {   
          for (j = i; j < n; j++)
    	
          {
    	  printf("The shortest distance between %d and %d is obtained by going through  ", i, j);
    	  fprintf(ofp,"%d ",i);
    	  getpath(i,j);
    	  fprintf(ofp, "%d \n ",j);
    	  printf("\n");
    	  	  
          }
      }
    fclose(ofp);
    Code:
    int getpath(int i, int j)
    {
    if (dist[i][j] == 0 )
    {
    
    }
    
    int intermediate = next[i][j];
    if (intermediate == 0)
    {
    
    }
    else
    {
    
    getpath(i, intermediate);
    printf("%d ", intermediate);
    getpath(intermediate, j);
    }
    return 0;
    }

    The problem is this. The get path utility is printing the intermediate nodes but I want those intermediate nodes but I want it to print those intermediate nodes in the path file. I have outputting the data from the getpath() routine, returning the values of k, and other stuff but nothing seems to be working. Please HELP!!!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hanniballector View Post

    The problem is this. The get path utility is printing the intermediate nodes but I want those intermediate nodes but I want it to print those intermediate nodes in the path file.
    Either:

    1) pass a FILE* to getpath() and print to it, or
    2) return a string or other value from getpath() and use that to print to a file.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    21
    Thankyou! I got it working properly. Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Saving results to a file
    By brian0918 in forum C++ Programming
    Replies: 10
    Last Post: 08-12-2002, 03:01 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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