By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,016 Members | 2,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,016 IT Pros & Developers. It's quick & easy.

Open File/Print Output File with in a loop

P: 1
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code:

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6. #include "util.h"
  7.  
  8. //Main Loop
  9. int main()
  10. {
  11.  
  12.    int   i, flag;
  13.    int   num_files;
  14.    int   len_str;
  15.    char  *idir = "/home/smurphy/data/20060117/";
  16.    char  **file_names;
  17.    char  *fullfn;
  18.    char  *output;
  19.  
  20.    //Define variables needed from the metar data:
  21.   char station[5];             //Station identifier
  22.    char weather[3];
  23.  
  24.    int snow_value;
  25.  
  26.    /*  Get list of files to be processed. */
  27.    flag = get_dir_list(idir, &num_files, &file_names);
  28.  
  29.    for(i=0; i<num_files; ++i){
  30.  
  31.       printf("file name #%d = %s\n", i, file_names[i]);
  32.       len_str = strlen(idir) + strlen(file_names[i]) + 1;
  33.       fullfn = calloc(len_str, sizeof(char));
  34.  
  35.       strcat(fullfn, idir);
  36.       strcat(fullfn, file_names[i]);
  37.       printf("fullfn = %s\n", fullfn);
  38.  
  39.       sprintf(output, "output%d.dat", i);
  40.  
  41.       FILE *input_stream;
  42.       FILE *output_stream;
  43.  
  44.       output_stream = fopen(output, "w");
  45.  
  46.       if (output == NULL){
  47.          printf("I could not open your output file.\n");
  48.       }
  49.  
  50.       //Opens input file
  51.       input_stream = fopen(fullfn,"r");
  52.  
  53.  
  54.       //Loop reads in input file and detemines at which stations snow is occuring at.
  55.  
  56.       while(fscanf(fullfn, "%s\t%s\n", station, weather) >= 1){
  57.  
  58.          if (strcmp(weather, "S") == 0){
  59.             snow_value = 2;
  60.             fprintf(output, "Stat: %s SigWx: %s SnoVal: %i\n", station, weather, snow_value);
  61.          }
  62.          else if (strcmp(weather, "S-") == 0){
  63.             snow_value = 1;
  64.             fprintf(output, "Stat: %s SigWx: %s SnoVal: %i\n", station, weather, snow_value);
  65.          }
  66.  
  67.          else if (strcmp(weather, "+S") == 0){
  68.             snow_value = 3;
  69.             fprintf(output, "Stat: %s SigWx: %s SnoVal: %i\n", station, weather, snow_value);
  70.          }
  71.  
  72.          else{
  73.             snow_value = 0;
  74.             fprintf(output, "Stat: %s  SigWx: None SnoVal: %i\n", station, snow_value);
  75.          }
  76.       }
  77.  
  78.    //Closes files
  79.    //fclose(fullfn);
  80.    //fclose(output);
  81.  
  82.    free(fullfn);
  83.    free (output);
  84.    }
  85.  
  86. }
  87.  

I am getting incompatible pointer types for arg 1 for my fscanf, fprintf. Can anyone help me out? I have never worked with opening/closing files within a loop, any suggestions?
Mar 29 '07 #1
Share this Question
Share on Google+
3 Replies


P: 3
This program worked in college where was made. Check how it is

:: Code removed per FAQ::

MWZ
Apr 2 '07 #2

sicarie
Expert Mod 2.5K+
P: 4,677
This program worked in college where was made. Check how it is

:: Code removed per FAQ::

MWZ
mweitz-

Please read our Posting Guidelines and abide by them. We ask that you not 1) post complete code or 2) give out code.

The OP has posted code, and it will help them more to show them where their program is not working, as they understand that better than what was posted, and they will learn more by fixing it.
Apr 3 '07 #3

P: 1
Well, this looks like homework but I am in a good mood.

The errors you were getting:

a.c:56: warning: passing arg 1 of `fscanf' from incompatible pointer type
a.c:60: warning: passing arg 1 of `fprintf' from incompatible pointer type
a.c:64: warning: passing arg 1 of `fprintf' from incompatible pointer type
a.c:69: warning: passing arg 1 of `fprintf' from incompatible pointer type
a.c:74: warning: passing arg 1 of `fprintf' from incompatible pointer type

I will give you a hint:

You declared fullfn and output as

char *fullfn;
char *output;

notice in your errors above the function that is being complained about...fscanf and fprintf. Hmmm...what does the "f" in fscanf and fprintf represent...Perhaps fullfn and output should be a different type of variable.
Apr 20 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.