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

C code and input files and output file

P: 1
Hi Everyone,
I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt
and compare them, the first text file has a bunch of values listed like:
y[1,1,1,1]
y[1,1,1,2]
y[1,1,1,3]
y[1,1,1,4]
y[1,1,1,5]
y[1,1,1,6]
y[1,1,1,7]
y[1,1,1,8]
and so on..
and I am trying to compare it with a text file that looks like:
C0001 1.0000000
C0215 1.0000000
C0817 1.0000000
C0078 1.0000000

where the C0001 is the first variable down from the top of the list and C0215 is the 215 th variable down from the list. So I want to find from the first list the 215th and output y[#,#,#,#} and the solution value 1.00000 or whatever is right of the C0215.

The problem I am having is that I can build my current C code below, but I when I debug it I get "UNC paths are not supported. Defaulting to Windows directory, Cannot open file"

below is my code:
/*
** FILE SOURCE6.C
**
** ASSOCIATED FILES TO BE READ:
** File swY.col
** File solution.txt
************************************************** **********************
** The file is opened for reading. Each line is successively fetched
** using fgets command. The string is then divided up and put into a matrix.
** Matrix will be of 4 dimensional
************************************************** **********************
** Note that fgets returns NULL when there are no more lines in the file.
************************************************** **********************
** In this file swY.col consists of various decesion variables.
************************************************** **********************
** Typical data in swY.col might be: y[1,1,1,1]
************************************************** **********************
** y[1,1,1,1] = y[d,t,k,i]=y[day,test,type,started at]
************************************************** **********************
** SO WE READ THE LINE FROM THE TEXT FILE, THEN WE CONVERT THE INTERGER
** VALUES TO VALUES IN THE MATRIX SO IF decesion variable is y[1,2,4,5]
** THEN THAT LOCATION IS [1,2,4,5] in the matrix
**
** In this file solution consists of various solution values for a column.
************************************************** **********************
** Typical data in solution.txt might be: C0001 1.00000000000
************************************************** **********************
** It would be desirious to read in the say C001 and know it value in a matrix
************************************************** **********************
** LATER THE ASSOCIATED DECESION VARIABLE WILL BE LINKED TO THE SOLUTION VALUE
** AND THEN OUTPUT WILL BE ADJUSTED AND PUT TO A WORD FILE OR ANOTHER TXT FILE.




*/

#include <stdlib.h>
#include <stdio.h> /*standard*/
#include <math.h> /*for later functionality*/
#include <string.h> /*for reading strings*/
#include <fstream> /*to write to a file*/
#define FILENAME "finaloutput.txt"


FILE *fr; /* declare the file pointer */
FILE *f_r; /*second file pointer*/
FILE *r_f;
int d, t, k, i;

int main(void)
{
/*location in matrix of value*/


/*y[d,t,k,i, index value] */
/*int decesion_var[1][1][1][1][1]; */

/* this will before reading in the column variable name*/
/* will be put into a matrix*/
/*char column;*/

/*int variable_name[1][1][1]; /* [Column, COLUMN NUMBER, SOLUTION VALUE]*/

/*double solution_value;*/

int counter;

/*open a file to read results too*/
/*FILE *file = fopen("finaloutput.txt", "w");*/
r_f = fopen(FILENAME , "w");



/* to read charchters from the txt. file*/
struct air
{
/* in the form of y[#,#,#,#]*/

char a[1]; /*'y'*/

char b[1];/*'['*/

int c;/*'day'*/

char p[1];/*','*/

int e;/*'test'*/

char f[1];/*','*/

int g;/*'type'*/

char h[1];/*','*/

int w;/*'started time'*/
char j[1];/*']'*/
};
struct air ia;

/* to read charchters from output file*/
struct ground
{
/* in the form of C####*/

char column[1]; /*'C'*/

int variable; /*'####'*/

double solution_value; /*solution value*/
};
struct ground ib;



fr = fopen ("swY40p10t3ctw45.col.txt", "r"); /* open the file for reading */
f_r= fopen ("solution.txt", "r"); /*open soultuion file for reading*/
/* "rt" means open the file for reading text */


if(f_r==NULL)

{

puts("Cannot open file");
/* exit file*/
exit(0);

}
/*********************************/
/*READS IN COLUMN and associated VALUES*/
/*********************************/
else{
/* check to see that your not at the end of the file*/
char line[1000];

/*while(fgets (f_r,"%column %variable %solution_value\n", ib.column, &ib.variable, &ib.solution_value) != EOF)*/
while( fgets(line,1000,f_r) != NULL)
{
/* scan in the value for that line*/

/* sscanf (line, &variable_name[&column,&variable,&solution_value]);*/
scanf(line,"%column %variable %solution_value", &ib.column,&ib.variable,&ib.solution_value);

/* at every iteration counter will be reset to 1, so it can go through the list*/

counter = 1;

/*check to make sure you can open the decision variable file*/
if(fr==NULL)
{

printf("Cannot open file\n");
/* exit file*/
exit(0);

}


/*********************************/
/*READS IN THE DECESION VARIABLES*/
/*********************************/

/* check to see that your not at the end of the file and that you have no exceeded 1000*/
char line_2[1000];
/*while((fgets(fr,"%a %b %c %p %e %f %g %h %w %j\n", ia.a, ia.b, &ia.c, ia.p, &ia.e, ia.f, &ia.g, ia.h, &ia.w, ia.j) != EOF) && (counter<= 1000))*/
while(fgets(line_2,1000,fr) != NULL)
{
/* scan in the line from the text file to comapre*/

scanf(line_2, "%a %b %c %p %e %f %g %h %w %j", &ia.a, &ia.b, &ia.c, &ia.p, &ia.e, &ia.f, &ia.g, &ia.h, &ia.w, &ia.j);

if((counter != ib.variable) && (counter <= 1000))
{
/* add to the counter*/
counter = counter + 1;
}
/*compare it with the value scaned in from the optimal soulution*/

else if(counter == ib.variable)
{
/*if this is true then write this vallue to a txt. file*/
/* the output will be in the form of:
y[#,#,#] # (optimal solution value)
*/
fprintf(r_f, "%a %b %c %p %e %f %g %h %w %j %solution_value\n", ia.a, ia.b, ia.c, ia.p, ia.e, ia.f, ia.g, ia.h, ia.w, ia.j, ib.solution_value);
/*output this value to the screen*/
printf( "%a %b %c %p %e %f %g %h %w %j \t\t\t %solution_value\n", ia.a, ia.b, ia.c, ia.p, ia.e, ia.f, ia.g, ia.h, ia.w, ia.j, ib.solution_value);
/* set counter to 1001 to force the false statment and exit the while loop*/
counter = 1001;
}

}

}

fclose (fr); /* close the file */

}

fclose(f_r); /* close the file prior to exiting the routine */

fclose(r_f); /* close txt file*/


return(0);
} /*of main*/
Jan 4 '08 #1
Share this Question
Share on Google+
1 Reply


hdanw
P: 61
Universal Naming Convention
Just a suggestion,

try to keep the post to the relavent information.

You have pages of useless information here and none of it helps resolve the issue.

It just puts a strain on those of us trying to help you.

Do you know how to trace the program? Can you isolate the few lines of code that generate the error?

Thats usually all we need.

I dont have the answer for you, and I bet a c-note that the person who does wont even have to look at your code to resolve your issue.

Happy coding.

Dan -
Feb 5 '08 #2

Post your reply

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