Actions

  Print Article
  BookMark Article

Categories    Category List

Arts & Humanities
  Books & Authors
  Dancing
  Genealogy
  History
  Performing Arts
  Philosophy
  Poetry
  Theater & Acting
  Visual Arts
Beauty & Style
  Fashion & Accessories
  Hair
  Makeup
  Skin & Body
Business & Finance
  Advertising & Marketing
  Careers & Employment
  Corporations
  Credit
  Insurance
  Investing
  Personal Finance
  Renting & Real Estate
  Small Business
  Taxes
Cars & Transportation
  Aircraft
  Boats & Boating
  Car
  Insurance & Registration
  Maintenance & Repairs
  Motorcycles
  Rail
Computers & Internet
  Computer Networking
  Hardware
  Internet
  Programming & Design
  Security
  Software
Consumer Electronics
  Camcorders
  Cameras
  Cell Phones & Plans
  Games & Gear
  Home Theater
  Music & Music Players
  PDAs & Handhelds
  TVs
Dining Out
Education & Reference
  Financial Aid
  Higher Education
  Preschool
  Primary & Secondary Education
  Special Education
  Studying Abroad
  Teaching
Entertainment & Music
  Celebrities
  Comics & Animation
  Horoscopes
  Jokes & Riddles
  Magazines
  Movies
  Music
  Polls & Surveys
  Radio
  Television
Environment
  Alternative Fuel Vehicles
  Conservation
  Global Warming
  Green Living
Family & Relationships
  Family
  Friends
  Marriage & Divorce
  Singles & Dating
  Weddings
Food & Drink
  Beer, Wine & Spirits
  Cooking & Recipes
  Ethnic Cuisine
  Vegetarian & Vegan
Games & Recreation
  Amusement Parks
  Board Games
  Card Games
  Gambling
  Hobbies & Crafts
  Toys
  Video & Online Games
Health
  Alternative Medicine
  Dental
  Diet & Fitness
  Diseases & Conditions
  General Health Care
  Men's Health
  Mental Health
  Optical
  Women's Health
Home & Garden
  Cleaning & Laundry
  Decorating & Remodeling
  Do It Yourself (DIY)
  Garden & Landscape
  Maintenance & Repairs
Local Businesses
News & Events
  Current Events
  Media & Journalism
Pets
Politics & Government
  Civic Participation
  Elections
  Embassies & Consulates
  Government
  Immigration
  International Organizations
  Law & Ethics
  Military
  Politics
Pregnancy & Parenting
  Adolescent
  Adoption
  Baby Names
  Newborn & Baby
  Parenting
  Pregnancy
  Toddler & Preschooler
  Trying to Conceive
Science & Mathematics
  Agriculture
  Astronomy & Space
  Biology
  Botany
  Chemistry
  Earth Sciences & Geology
  Engineering
  Geography
  Mathematics
  Medicine
  Physics
  Weather
  Zoology
Social Science
  Anthropology
  Dream Interpretation
  Economics
  Gender Studies
  Psychology
  Sociology
Society & Culture
  Community Service
  Cultures & Groups
  Etiquette
  Holidays
  Languages
  Mythology & Folklore
  Religion & Spirituality
  Royalty
Sports
Travel

Online Now    Online Now

Author Login    Author Login

Welcome Guest! Please login or create an account.

Username:

Password:



Navigation    Navigation

ADS    Featured Author

ad

ADSDisclosure

C++ to C Code?

Author :

Submitted : 2018-06-15 15:22:44    Popularity:     

Tags: Code  

I am writing a C code that reads numbers from a file and I am stuck on how to write the following in C, I know the C++ equivalent which is what I have here:

ifstream inFile;

inFile >> rows;
inFile >> mat

Answers:

Here's a simple example:

/*
. * Read the specified input text file that contains the dimensions
. * and contents of a matrix of integers. The first line is:
. * rows columns
. * Subsequent rows contain the matrix data.
. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STR_LEN 1024
#define WHITESPACE " ,\t"

FILE *openFile(const char *prompt, const char *mode);

int main(int argc, char *argv[]) {
. . char numstr[MAX_STR_LEN],*s;
. . int n, rows, cols, i, j, **matrix;
. . FILE *in;

. . if ((in = openFile("Enter input file name: ","r")) != NULL) {
. . . . s = fgets(numstr,MAX_STR_LEN,in);
. . . . if ((sscanf(s,"%d %d",&rows, &cols) == 2) && (rows > 0) && (cols > 0)) {
. . . . . . matrix = calloc(rows, sizeof(int *));
. . . . . . for (i = 0; i < rows; i++) {
. . . . . . . . matrix[i] = calloc(cols, sizeof(int));
. . . . . . . . s = fgets(numstr, MAX_STR_LEN, in);
. . . . . . . . *(strchr(numstr,'\n')) = '\0';
. . . . . . . . if (strlen(s) > 0) {
. . . . . . . . . . j = 0;
. . . . . . . . . . while ((s != (char *)NULL) && (j < cols)) {
. . . . . . . . . . . . if (sscanf(s,"%d",&n) == 1) {
. . . . . . . . . . . . . . matrix[i][j++] = n;
. . . . . . . . . . . . }
. . . . . . . . . . . . if ((s = strpbrk(s, WHITESPACE)) != (char *)NULL) {
. . . . . . . . . . . . . . s = &s[strspn(s, WHITESPACE)];
. . . . . . . . . . . . }
. . . . . . . . . . } /* end for each column in row i */
. . . . . . . . } /* end if not empty line */
. . . . . . } /* end for each row */
. . . . . . for (i = 0; i < rows; i++) {
. . . . . . . . for (j = 0; j < cols; j++) {
. . . . . . . . . . printf("%d ", matrix[i][j]);
. . . . . . . . }
. . . . . . . . puts("");
. . . . . . }
. . . . } else {
. . . . . . puts("first line in file must be: rows cols");
. . . . }
. . } else {
. . . . puts("input file not found");
. . }
. . fclose(in);
. . return 0;
}

FILE *openFile(const char *prompt, const char *mode) {
. . FILE *fp = NULL;
. . char fileName[MAX_STR_LEN];

. . printf("%s",prompt);
. . fgets(fileName,MAX_STR_LEN,stdin);
. . *(strchr(fileName,'\n')) = '\0';
. . if ((fp = fopen(fileName,mode)) == NULL) {
. . . . printf("\nerror opening %s\n",fileName);
. . }
. . return fp;
}

#if 0

Sample run:

Enter input file name: matrixData.txt
10 20 30 40
50 60 70 80


Where:

$ cat matrixData.txt
2 4
10 20 30 40
50 60 70 80

#endif

C is just C++ without the built-in stuff for objects. You can still do objects in C, but it's a lot uglier.

Will you provide code that compiles?

How to convert C++ Code to C - Stack Overflow
https://stackoverflow.com/questions/7372...



Good
Back Homepage
Back


Article Source:
www.Aphotolog.Com

Answer Questions