473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Open File/Print Output File with in a loop

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
3 4633
mweitz
3
This program worked in college where was made. Check how it is

:: Code removed per FAQ::

MWZ
Apr 2 '07 #2
sicarie
4,677 Expert Mod 4TB
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
Clfy
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

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

Similar topics

0
by: Sure | last post by:
I my php script I call a perl script that perl script will create a log file From the php script I want to read the log file and write the output to the browser. This operation has to carried out...
0
by: Danny Anderson | last post by:
Hola, C++ folk! I want to have a do...while loop that works with a different file each iteration, prompting the user for the file name to open and write. The problem I am having is that the...
1
by: Andrew | last post by:
I'm adding this as it to me a while to figure out all the pieces to be able to do this without using Microsoft.Office.Interop which caused me problems on the web-server. Streaming is the easy...
8
by: js | last post by:
Hello, list. I have a list of sentence in text files that I use to filter-out some data. I managed the list so badly that now it's become literally a mess. Let's say the list has a sentence...
21
by: Umesh | last post by:
/*program to search a* in a text file & write output in a file.* indicated any character*/ #include<stdio.h> #include<stdlib.h> int main(void) { FILE *f,*fp; f=fopen("c:/1.txt","r");...
12
by: webinfinite | last post by:
I am starting a new thread for this topic since previous problem has been solved. I have code like this: #include <stdio.h> int main(){
1
by: sunnyluthra1 | last post by:
Hi Everyone, I am working on a piece of code, that displays the properties(Name, Datatype, size & Description) of the table in the database. Now I want to further Enhance the code. I Have created...
0
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work...
2
by: rparimi | last post by:
I am trying to redirect stderr of a process to a temporary file and then read back the contents of the file, all in the same python script. As a simple exercise, I launched /bin/ls but this doesn't...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.